title: Cap string to a certain length directly without a function
body: <p>Not a duplicate of <a href="https://stackoverflow.com/q/2776673/3785314">this</a>.</p>
  
  <p>I want to make a string have a max length. It should never pass this length. Lets say a 20 char length. If the provided string is > 20, take the first 20 string and discard the rest. </p>
  
  <p><a href="https://stackoverflow.com/q/2776673/3785314">The</a> answers on that question shows how to cap a string with a function but I want to do it directly without a function. I want the string length check to happen each time the string is written to.</p>
  
  <p>Below is what I <strong>don't</strong> want to do:</p>
  
  <pre><code>string myString = "my long string";
  myString = capString(myString, 20); //&lt;-- Don't want to call a function each time
  
  string capString(string strToCap, int strLen)
  {
      ...
  }
  </code></pre>
  
  <p>I was able to accomplish this with a property:</p>
  
  <pre><code>const int Max_Length = 20;
  private string _userName;
  
  public string userName
  {
      get { return _userName; }
      set
      {
          _userName = string.IsNullOrEmpty(value) ? "" : value.Substring(0, Max_Length);
      }
  }
  </code></pre>
  
  <p>Then I can easily use it whout calling a function to cap it:</p>
  
  <pre><code>userName = "Programmer";
  </code></pre>
  
  <p>The problem with this is that every <code>string</code> I want to cap must have multiple variables defined for them. In this case, the <code>_userName</code> and the <code>userName</code> (property) variables.</p>
  
  <p>Any clever way of doing this without creating multiple variables for each string and at the-same time, not having to call a function each time I want to modify the <code>string</code>?</p>