Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think a bit of code will help illustrate what setters and getters are:</p> <pre><code>public class Foo { private string bar; public string GetBar() { return bar; } public void SetBar(string value) { bar = value; } } </code></pre> <p>In this example we have a private member of the class that is called bar. The GetBar and SetBar methods do exactly what they are named - one retrieves the bar member, and the other sets its value.</p> <p>In c# 1.1 + you have properties. The basic functionality is also the same:</p> <pre><code>public class Foo { private string bar; public string Bar { get { return bar; } set { bar = value; } } } </code></pre> <p>The private member bar is not accessible outside the class. However the public "Bar" is, and it has two accessors - get, which just as the example above "GetBar()" returns the private member, and also a set - which corresponds to the SetBar(string value) method in the forementioned example.</p> <p>Starting with C# 3.0 and above the compiler became optimized to the point where such properties do not need to have the private member as their source. The compiler automatically generates a private member of that type and uses it as a source of a property.</p> <pre><code>public class Foo { public string Bar { get; set; } } </code></pre> <p>what the code shows is an automatic property that has a private member generated by the compiler. You don't see the private member but it is there. This also introduced a couple of other issues - mainly with access control. In C# 1.1, and 2.0 you could omit the get or set portion of a property:</p> <pre><code>public class Foo { private string bar; public string Bar { get{ return bar; } } } </code></pre> <p>Giving you the chance to restrict how other objects interact with the "Bar" property of the Foo class. Starting with C# 3.0 and above - if you chose to use automatic properties you would have to specify the access to the property as follows:</p> <pre><code>public class Foo { public string Bar { get; private set; } } </code></pre> <p>What that means is that only the class itself can set Bar to some value, however anyone could read the value in Bar.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload