title: Difference between public {get; set} and programming getters and setters
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4142867/what-is-difference-between-property-and-variable-in-c-sharp">What is Difference between Property and Variable in C#</a>  </p>
  </blockquote>
  
  
  
  <p>I started working with C# a few weeks ago, and this is something that has really been bugging me. C# allows these so-called 'magic' getters and setters, also known as 'syntactical sugar'. So, I can do something like this:</p>
  
  <p><code>public int myInt { get; set; }</code></p>
  
  <p>But from an encapsulation standpoint, this is pointless. For one, the data member is <strong>public</strong>, and I can get/set it using the dot operator. However, if I do this:</p>
  
  <p><code>private int myInt { get; set; }</code></p>
  
  <p>I can't access it at all, as <code>myInt is inaccessible due to protection level</code>. What is this <em>actually</em> doing? I thought this was supposed to be an easy way to accomplish data encapsulation, so I wouldn't have to do this:</p>
  
  <pre><code>private int myInt;
  public void setMyInt(int i) { myInt = i; }
  public int getMyInt() { return myInt; }
  </code></pre>
  
  <p>But it's not. As near as I can tell, I'm just making these variables public. I thought maybe I would be able to do something like</p>
  
  <p><code>public int myInt { get; }</code></p>
  
  <p>So the client could <code>get</code> it, but not <code>set</code> it, but no, public access is still allowed. So what gives? </p>
  
  <p><strong>EDIT</strong>
  I'm not trying to do anything specific, I just want to understand how this actually works. To clarify:</p>
  
  <p>Making the variable <code>public</code> doesn't accomplish encapsulation, especially when I can access it with the dot operator. Writing getters and setters for a <code>private</code> variable allows you to make changes to the variable, but gives you greater control over how that actually happens.</p>