title: How can I put validation in the getter and setter methods in C#?
body: <p>In C#, I can have a property without having the need to declare a private variable.
  My VB6 code that looked like this</p>
  
  <pre><code>'local variable(s) to hold property value(s)
  Private mvarPhoneNumber As String 'local copy
  Public Property Let PhoneNumber(ByVal vData As String)
  'used when assigning a value to the property, on the left side of an assignment.
  'Syntax: X.PhoneNumber = 5
      mvarPhoneNumber = vData
  End Property
  
  
  Public Property Get PhoneNumber() As String
  'used when retrieving value of a property, on the right side of an assignment.
  'Syntax: Debug.Print X.PhoneNumber
      PhoneNumber = mvarPhoneNumber
  End Property
  </code></pre>
  
  <p>can now look like this.</p>
  
  <pre><code>public string PhoneNumber{get;set;}
  </code></pre>
  
  <p>How can I put validation in the getter and setter methods in C#?
  I tried adding a validation like this.</p>
  
  <pre><code>public string PhoneNumber
          {
              get
              {
                  return PhoneNumber;
              }
              set
              {
                  if (value.Length &lt;= 30)
                  {
                      PhoneNumber = value;
                  }
                  else
                  {
                      PhoneNumber = "EXCEEDS LENGTH";
                  }
              }
          }
  </code></pre>
  
  <p>The get part of this code won't compile.
  Do I need to revert to using a private variable?</p>