开发者

C# , Difference between property with variable and without variable [duplicate]

开发者 https://www.devze.com 2023-04-02 10:16 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: What's the difference between encapsulating a private member as a property and defining a property without
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What's the difference between encapsulating a private member as a property and defining a property without a private memb开发者_运维问答er?

I know the basic functionality of properties . But as i go through documentation in depth i see they are declared just with get set and without a variables .

what is the diffeence between these two

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

and

public int EmpCode
{
    get; 
    set; 
}  

Is it just a easier way of writing which got as .net frameworks got upgraded . Or is there any functional difference ?


The later is called an Automatic Property and is the same.They were introduced in C#3, you can read more about them here: http://trashvin.blogspot.com/2008/05/automatic-properties-and-object.html

Simply put, Automatic Properties are syntactic sugar so the developer has to type less code and the compiler will generate the private field and the public setter and getter for you.


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

Section 10.7.3 of the C# specification gives more detail:

When a property is specified as an automatically implemented property, a hidden backing field is automatically available for the property, and the accessors are implemented to read from and write to that backing field.

The following example:

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

is 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; } }
}


It's called Auto-properties and is just a easier way of writing them if you don't need any logic inside the property. When compiled, the compiler will auto-generate a backing variable for the property so it's exactly the same.

It's easy to change the property into a property with a backing field and some logic later on, and that will not break any code dependant on this property, If you instead just used a public field first, and then changed it into a property you would break the code that relied on this field/property.


Second way is actually auto-property which implicitly implement backing field, which means that you cannot affect generated field name.

Most of the time you don't care, however in scenarios when you pass objects between layers using serializing / deserializing , you in some cases would have explicitly created backing field, in order to get rid of __propBackingField2735t34 like names on the client.

Also in explicitly coded properties some logic for validation can be included which is not the case for autoproperites


This is a enhancement from C# 3.0 which is termed as auto implemented properties, a private backing field is automatically created by the compiler in the background


there is no functional difference as such.. but if you want more functionality on your property setting/getting you may use the version with the 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 the private variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消