Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a short & simple example of getters/setters in C#

I am having trouble understanding the concept of getters and setters in the C# language. In languages like Objective-C, they seem an integral part of the system, but not so much in C# (as far as I can tell). I have read books and articles already, so my question is, to those of you who understand getters & setters in C#, what example would you personally use if you were teaching the concept to a complete beginner (this would include as few lines of code as possible)?

like image 720
CM90 Avatar asked Jun 22 '12 15:06

CM90


People also ask

What is the new 2022 short haircut?

Pixie cut. The Pixie haircut, which is very short and peaked, with the sides neatly trimmed and the back of the neck, is a great bet for next year. It ensures an extremely modern and stylish look, and suits all hair types, whether straight, wavy, curly or frizzy.

What is the best short haircut for a 55 year old woman?

An undercut pixie is one of the best age-defying short hairstyles for women over 50. Tousled long pixie hairstyles have this youthful appeal and sassiness females over 50 are looking for. Sweep the bangs to any side for a chic effect.


2 Answers

I think a bit of code will help illustrate what setters and getters are:

public class Foo {    private string bar;     public string GetBar()    {        return bar;    }     public void SetBar(string value)    {        bar = value;    } } 

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.

In c# 1.1 + you have properties. The basic functionality is also the same:

public class Foo {     private string bar;      public string Bar     {         get { return bar; }         set { bar = value; }     } } 

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.

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.

public class Foo {    public string Bar { get; set; } } 

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:

public class Foo {     private string bar;      public string Bar     {         get{ return bar; }     } } 

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:

public class Foo {     public string Bar { get; private set; } } 

What that means is that only the class itself can set Bar to some value, however anyone could read the value in Bar.

like image 124
bleepzter Avatar answered Oct 13 '22 09:10

bleepzter


In C#, Properties represent your Getters and Setters.

Here's an example:

public class PropertyExample {     private int myIntField = 0;      public int MyInt     {         // This is your getter.         // it uses the accessibility of the property (public)         get         {             return myIntField;         }         // this is your setter         // Note: you can specify different accessibility         // for your getter and setter.         protected set         {             // You can put logic into your getters and setters             // since they actually map to functions behind the scenes             if (DoSomeValidation(value))             {                 // The input of the setter is always called "value"                 // and is of the same type as your property definition                 myIntField = value;             }         }     } } 

You would access this property just like a field. For example:

PropertyExample example = new PropertyExample(); example.MyInt = 4; // sets myIntField to 4 Console.WriteLine( example.MyInt ); // prints 4 

A few other things to note:

  1. You don't have to specifiy both a getter and a setter, you can omit either one.
  2. Properties are just "syntactic sugar" for your traditional getter and setter. The compiler will actually build get_ and set_ functions behind the scenes (in the compiled IL) and map all references to your property to those functions.
like image 22
Jon Senchyna Avatar answered Oct 13 '22 10:10

Jon Senchyna