I think it was in .net 2.0, microsoft introduced an accessor that was abbreviated to something like
public string Name { get; set; }
But is there any real difference between the above code, and simply:
public string Name;
A get property accessor is used to return the property value, and a set property accessor is used to assign a new value. In C# 9 and later, an init property accessor is used to assign a new value only during object construction. These accessors can have different access levels.
Public accessors allow any object to read and write these properties. It's sometimes desirable, however, to exclude one of the accessors. You can omit the set accessor to make the property read-only: C# Copy. public string Name { get { return _name; } private set { _name = value; } }
public - can be access by anyone anywhere. private - can only be accessed from with in the class it is a part of. protected - can only be accessed from with in the class or any object that inherits off of the class.
An Accessors just returns value of one of the member variables . It is a member function that accesses the contents of the object but does not modify the objects. A Mutator just assign a new value to one of the member variable. It is a member function that can modify an object.
The main difference is that if you later need to add logic into your getter or setter, and other DLLs have already been compiled against yours, you can easily change
public string Name { get; set; }
into
public string Name { get{/*special code*/} set{/*special code*/} }
and it won't be a breaking change to publish your new DLL and have other DLLs not be recompiled.
Whereas if you changed
public string Name;
into
public string Name { get{/*special code*/} set{/*special code*/} }
then you'll need to make sure any DLLs that use yours are recompiled, since they change from accessing a field into accessing a property.
This is obviously a bigger problem when you're shipping DLLs to other programmers (as an open source project or as a component vendor for example) than if you're just building an app for your own self / employer
The difference is between a Field and a Property. A field is just a member variable on the class instance. By contrast, a property is shorthand for two separate actions - get and set:
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
private string _name;
This is an over-simplified example, as the property simply "wraps" the private field by returning it in the getter and setting it in the setter. However, properties become very useful when they become "gateways" to the underlying value. If program flow requires something happen every time the value of a field is set (say, an event is fired), it can be fired from the setter of the property:
set
{
this.InvokePropertyChangedEvent();
_name = value;
}
The exact syntax you're asking about is called Auto-Implemented Properties, which is just shorthand for the simple example I provided above. The compiler creates a private member that is got and set by the property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With