Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property with and without { get; set; }

Tags:

c#

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

like image 280
Gautam Jain Avatar asked Mar 05 '11 10:03

Gautam Jain


People also ask

Why we use get and set with properties?

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.

What does get set do?

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.

Why we use get set property in C#?

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.

When property contains only get method then we get?

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.


4 Answers

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.

like image 98
Oded Avatar answered Sep 21 '22 02:09

Oded


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

like image 32
Marc Gravell Avatar answered Sep 23 '22 02:09

Marc Gravell


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.

like image 38
John Gietzen Avatar answered Sep 24 '22 02:09

John Gietzen


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#?

like image 25
Jon Avatar answered Sep 20 '22 02:09

Jon