title: Why do I need to use get and set?
body: <p>I have a code segment:</p>
  
  <pre><code>public class MyClass
  {
      private string _myProperty;
  
      public string MyProperty
      {
          get
          {
              return _myProperty;
          }
  
          set
          {
              _myProperty = value;
          }
      }
  }
  </code></pre>
  
  <p>What is the point here? I could have declared the <code>_myProperty</code> string as public and any of my class objects will be able to directly access them and get or set the value.</p>
  
  <p>Instead, we are making <code>_myProperty</code> private and the using get and set to access them, using the class object.</p>
  
  <p>In either case, the class object is able to access them and the result is always same.</p>
  
  <p>So why use this approach? Is that only because I can implement few constraints in setter?</p>
  
  <p>Apart from that what harm will making member variables public cause? In this example I could expose <code>_myProperty</code> as public instead of restricting it to this class only by making it private, as OOP would suggest.</p>