1

I'm an aspiring student who in future aims to pursue a career in computer science. Ive been reading documentation for quite a bit now, and i understand fundamentals of OOP, But im wondering exactly how the {Get ; Set;} Methods are used in c#. Thanks in advance.

E.G.

public class Car
{
    public Name { get; set; }
}
ekad
  • 14,436
  • 26
  • 44
  • 46
Needham
  • 457
  • 1
  • 6
  • 15

2 Answers2

5

This public string Name { get; set; } is called an auto implemented property. The compiler will create a private backing field that will hold the value we will set using this property. Furthermore, it will create two methods, one for the set (setting the value) and one for the get (getting the value) of this value. You could consider that set is an assignment and get is a read. So when we say that we set a value actually we mean we assign a value to a variable. While when we say that we get a value actually we mean that we read the value that is stored in a variable.

Furthermore, this public string Name { get; set; } is equivalent to the following:

private string name;
public string Name
{
    get { return name; }
    set { name = value; }
}

Comparing one another the first one does the same job with fewer lines of code. However, in both cases C# compiler will create as I stated above one method for setting the value and one method for getting the value. The main difference, it is that in the second case, the creation of backing field isn't needed.

Update

In order to be more clear the above , I created a console application, in which I added a class called Customer with the following definition:

class Customer
{
    public string Name { get; set; }
}

I run my fantastic application and then I looked at the MSIL code that had been created by the C#.

enter image description here

I created the above using a free .net disassembler, called Ildasm.exe (IL Disassembler). This tool as it's name implies shows the IL code C# compilers creates, when we build one application. A good tutorial about this tool can be found here.

As you notice the type Customer is compiled to a class that has a backing field and two methods, get_Name and the set_Name. These have been created automatically by the C# compiler.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • 1
    I think for a new programmer it would be good to show a code example of the sentence *"Furthermore, it will create two methods, one for the set and one for the get of this value."*. I know, when I was learning, once that clicked for me it became much more clear. – Scott Chamberlain Nov 18 '14 at 14:56
  • The advantage you get from that is that you can restrict only the Setter or only the Getter pretty fast without doing a lot of stuff around this. – Horius Nov 18 '14 at 14:56
  • @ScottChamberlain that I am doing currently now. I will edit my post in a few minutes. Thank you ! – Christos Nov 18 '14 at 14:58
  • Might be worth mentioning access modifiers on get; set; too – jaywayco Nov 18 '14 at 20:07
1

I don't program in C#, but get and set methods work the same way in most OOP languages. It has to do with the encapsulation concept in Object Oriented Programming. You declare a data member of a class private, and then access it using a get method, and change it using a set method. This hides the actual implementation of these methods from the client/user, so it hides the data.

Here is a simple Java example:

public class SomeClass {
    private int data;

    public int getData() {
        return data;
    }

    public void setData(int newValue) {
        data = newValue; // Hidden from the user
    }
}
Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35