The difference between public {get; set} and programming getters and setters

Possible duplicate:
What is the difference between a property and a variable in C #

I started working with C # a few weeks ago, and this is what really listened to me. C # allows the use of so-called “magic” getters and setters, also known as “syntactic sugar”. So, I can do something like this:

public int myInt { get; set; }

But in terms of encapsulation, this is pointless. Firstly, the data item is publicly available , and I can get / set it using the point operator. However, if I do this:

private int myInt { get; set; }

I cannot access it at all, as myInt is inaccessible due to protection level . What is this actually doing? I thought this should be an easy way to perform data encapsulation, so I would not have to do this:

 private int myInt; public void setMyInt(int i) { myInt = i; } public int getMyInt() { return myInt; } 

But this is not so. As far as I can tell, I'm just making these variables publicly available. I thought maybe I could do something like

public int myInt { get; }

That way, the client could get it, but not set , but no, public access is still allowed. So what gives?

EDIT I'm not trying to do something specific, I just want to understand how it works. To clarify:

Creating a public variable does not perform encapsulation, especially when I can access it using the point operator. Writing getters and setters for the private variable allows you to make changes to the variable, but gives you more control over how this actually happens.

+4
source share
5 answers

The goal is that you support encapsulation with future modifications.

If you are initially writing your class using automatic getters and setters:

 public int Count { get; set; } 

Then it will support exactly the same external interface if you then change it to

 public int Count { get { /* very complicated logic */ } set { /* even more complicated logic */ } } 

Automatic only to help you with simple properties in the beginning.

+6
source

Are you trying to write

 public int MyInt { get; private set; } 

EDIT : The point of automatically implemented properties should not provide additional encapsulation, but to avoid fields.
If you create a class with an open field ( publit int MyInt; ), and someone uses this field, you will not be able to change it later for the property, otherwise you will break any compiled assemblies that use it.

Using an auto-property gives you the same simplicity and brevity as a field, but allows you later to replace it with a full-scale property (containing additional logic) without breaking anything.

+6
source

These “magic” getters and setters, as you call them, actually end up creating methods just like they do in Java. So, encapsulation is only supported with a cleaner look, IMO.

The properties themselves, even if they are declared as public int Property {get;set;} , for example, have a support field and getter and setter methods.

See this contrived example:

 class A { public int ID {get;set;} } 

Now that you are doing something like:

 A a = new A(); a.ID=5; 

The generated IL code looks like this:

 A.get_ID: //getter method IL_0000: ldarg.0 IL_0001: ldfld UserQuery+A.<ID>k__BackingField IL_0006: ret A.set_ID: //setter method IL_0000: ldarg.0 IL_0001: ldarg.1 IL_0002: stfld UserQuery+A.<ID>k__BackingField IL_0007: ret 

So yes, a.ID=5 ; it looks like it accesses the member variable directly, but actually calls the method; in this case, the set_ID method.

+5
source

You just post it - if you want to specify a private set, you can do something like this:

  int myInt { get; private set; } 
0
source

you can do this, which is very useful:

public int myInt {get; private recruitment; }

0
source

All Articles