C #, Difference between property with variable and without variable

Possible duplicate:
What is the difference between encapsulating a private element as a property and defining a property without a private participant?

I know the basic functionality of properties. But as I am parsing the documentation in detail, I see that they are declared only with set and without variables.

what is the difference between these two

public int EmpCode { get { return _strEmpCode; } set { _strEmpCode = value; } } 

and

 public int EmpCode { get; set; } 

This is just an easier way to write, which got as .net frameworks updated. Or is there any functional difference?

+8
c # properties
source share
6 answers

Later it is called an automatic property and is one and the same. They were introduced in C # 3, you can read more about them: http://trashvin.blogspot.com/2008/05/automatic-properties-and-object.html

Simply put, "Automatic Properties" is syntactic sugar, so the developer has to enter less code, and the compiler will generate a private field and a public setter and recipient for you.

+6
source share

This is called an automatic property. There is no functional difference. The latter syntax is only a shorthand for the former.

Section 10.7.3 of the C # specification provides more details:

When a property is specified as an automatically implemented property, a hidden support field is automatically available for the property, and accessors are implemented for reading and writing to this background field.

The following example:

 public class Point { public int X { get; set; } // automatically implemented public int Y { get; set; } // automatically implemented } 

equivalent to the following declaration:

 public class Point { private int x; private int y; public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } } 
+3
source share

It is called Auto-Properties and is just an easier way to write them if you don't need the logic inside the property. When compiled, the compiler will automatically generate a reference variable for the property so that it is exactly the same.

It is easy to change a property into a property with an additional field and some logic later, and this will not break any code that depends on this property. If instead you only used a public field, and then changed it to a property that you would violate the code that relied on this field / property.

0
source share

there is no functional difference as such .. but if you want more functionality in setting / getting your property, you can use the version with private variables ..

  public int EmpCode { get { return _strEmpCode > 0 ? 100 + _strEmpCode : 0; } set { if (value > 0) _strEmpCode = value; } } 

Otherwise, you can simply use the version without private variables.

0
source share

The second way is actually an auto-property that implicitly implements the support field, which means that you cannot influence the generated field name.

In most cases, you don’t care, but in scenarios when you transfer objects between layers using serialization / deserialization, in some cases you explicitly created a support field to get rid of __propBackingField2735t34 such names on the client.

Also, in the explicitly encoded properties, some logic can be included to check that it does not apply to auto-impregnations

0
source share

This is an extension from C # 3.0 called automatic implemented properties, a private support field is automatically created by the compiler in the background

0
source share

All Articles