What is the difference between public int i and public int i {get; set;} (what is the difference between an automatic property and a public member?)

Possible duplicates:
C #: why is there an empty get set property instead of using the public variable? C #: open fields and automatic properties

I use โ€œautomaticโ€ properties in my code, and I wonder what the actual difference between this code is:

public class foo{ public int i; } 

and

 public class foo{ public int i {get; set;} } 

I know there is a difference, because the third party sines I used were skipped by the public members, but found them after adding {get; set;} {get; set;} .

AS there is no private field, what happens behind the scenes?

+4
source share
3 answers

A private field is generated by the compiler when using automatic properties .

When you declare a property, as shown in the following example, the compiler creates a private, anonymous support field that can only be accessed through the get and set accessors property.


Regarding the difference between the two examples, the first reveals the field directly for manipulation. This is considered bad practice (I think the information is hiding, loss of encapsulation).

In the second example, you should use getter and setter, and you can add any checks and other logic to them.

See this blog post:

If I have a field with special behavior, should I write the property "just in case" (with the trivial get / set), or should I open a public field?

The reason the library design guide suggests you write a property is because it is important that libraries be an easy version. If you put the property there ahead of time, you can change the implementation of the property without requiring the user to recompile your code.

+5
source

This is an auto property, not an anonymous property. In fact, there is a private support field for it, it is simply generated automatically by the compiler and is not available to you at compile time. If you run your class using Reflector (or view it at run time with reflection), you will see a support box.

To answer your question, โ€œWhat's the difference?โ€, The obvious answer is that one is a field, while one is a property. The advantage of using auto properties is that it gives you the opportunity to switch to traditional properties later, if the need arises, without changing your API. Since third-party code is capable of โ€œreachingโ€ one and not the other, this will be the question that the other developer will best answer. However, most APIs are designed to work with properties, not with fields (since the common wisdom is that you do not open fields outside the declaring class). If a third-party library smoothly scans your class, it is most likely looking for properties.

It is important to remember that:

 private string backingField; public string Data { get { return backingField; } set { backingField = value; } } 

and

 public string Data { get; set; } 

Essentially the same code compiled. The only significant difference is the name of the support field.

+1
source

The first field is a field and can be described as a POD. The second is a property and allows you to multiply derived classes and Shadow, while the first does not. In addition, the second is subtlety, as the correspondent silently creates a backup store.

+1
source

Source: https://habr.com/ru/post/1336375/


All Articles