Open access to .net network

I think it was in .net 2.0, microsoft introduced an accessory that was reduced to something like

public string Name { get; set; }

But is there any real difference between the above code and just:

 public string Name; 
+6
c #
source share
7 answers

The main difference is that if you need to add logic to your getter or setter, and other DLLs are already compiled against you, you can easily change

 public string Name { get; set; } 

in

 public string Name { get{/*special code*/} set{/*special code*/} } 

and it will not be a violation of changes to publish your new DLL and not recompile other DLLs.


If you changed

 public string Name; 

in

 public string Name { get{/*special code*/} set{/*special code*/} } 

then you will need to make sure that all the DLLs that use yours are recompiled as they change from accessing the field to accessing the property.

This is obviously a big problem when you send DLLs to other programmers (for example, an open source project or as a component provider) than if you just create an application for your own / employer

+6
source share

The difference between Field and Property . A field is only a member variable in an instance of a class. On the contrary, the property is a shorthand for two separate actions - get and set:

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

This is an overly simplified example, since the property simply wraps the private field by returning it to getter and setting it to setter. However, properties become very useful when they become “gateways” to the base value. If the program flow requires that something happens every time a field value is specified (for example, an event is triggered), it can be started from the property settings tool:

 set { this.InvokePropertyChangedEvent(); _name = value; } 

The exact syntax you are asking for is called Auto-Implemented Properties , which is just a shorthand for the simple example that I set out above. The compiler creates a private member that is obtained and set by the property.

+4
source share

Automatic properties were first introduced in C # 3.0. Difference between:

 public string Name { get; set; } 

and

 public string Name; 

lies in the fact that the first declares a property and the second a field . OOP properties are used to encapsulate fields. A property can have setter or getter or both, and you can also specify different levels of accessibility for each.

+4
source share

The difference between declaring a shorthand property is that you can define it that way.

 public string Name { get; private set; } 

This means that a property can be read publicly, but only private members can be written to it. You cannot do such a thing for the field.

If you look at the generated short property declarations IL, you will find that the compiler has added / autogenerated member fields to the property to read or write.

+2
source share

There are no functional differences in writing code to get the value or save it. But there are times when the caller can expect a field or property and will only accept one or another using reflection. For example, WPF can only bind to a property, not to a field.

+2
source share

Yes, the code in the second line you provide directly a member in memory, while in the first line you have one level of indirection in which you can add some logic in the future for verification and lazy assignment.

Also, if you use reflection, you will need to look for Setterter and Getter for the first line of the example, and for the second you will need to directly get the member variable.

Usually using properties is much better.

+1
source share

One of the useful differences I found for propertygrid users was that using public string Name {get; set; } we could easily set the source data in the Propertygrid.

when declaring a public string Name; will not be used for Propertygrid.

0
source share

All Articles