I am new to C#
What is the difference between
public string MyValue;
and
public string MyValue { get; set; }
I always assumed that both were same. Something was not working in my code. Using the latter form made it work. But don't know what is the difference. Can you help?
Thanks
Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code. A get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels.
Example explained The get method returns the value of the variable name . The set method assigns a value to the name variable. The value keyword represents the value we assign to the property.
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.
There are two type of accessors i.e. get accessors and set accessors. There are different types of properties based on the “get” and “set” accessors: Read and Write Properties: When property contains both get and set methods. Read-Only Properties: When property contains only get method.
The first is a public field, the second an automatically implemented public property.
They are not the same. With the auto implemented property the compiler will generate a private backing field.
Though both can work as a way to expose data from your class, you should be using properties following the principle of information hiding - fields should be private and only accessed through properties. This allows you to make changes to the implementation without breaking the callers.
If the "latter made it work", you are probably using data-binding; data-binding usually works only against properties (not fields). These can be explicit properties, or automatically implemented properties like in your example.
Note that changing from a field to a property can break serialization if you are using BinaryFormatter
(which IMO is deeply flawed anyway), but properties are very much preferred over fields. Absolutely make this change ;p
Those are actually very different constructs.
This form is the only way to actually allocate memory for data:
string MyData;
This is called a "field".
This form is called an "automatically implemented property":
string MyData { get; set; }
The compiler translates this onto something like this:
string myDataField;
string MyData
{
get { return myDataField; }
set { myDataField = value; }
}
So as you can see they are very different, yet they both end up creating a field for storage. However, using the property allows for much more future flexibility.
The first one is a field, not a property. Have a look at this question:
What is the difference between a Field and a Property in C#?
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