开发者

What is the difference between prop and a full property?

Is there any difference between the two pieces of code below? Or is the top just a short form of the bottom one?

public string Name { get; set; }

and

private string _Name;
public string Name
{
    get { return _Name;开发者_开发技巧 }
    set { _Name = value; }
}


The only difference (other than the fact you would have to do the initialization with "Default Name" in your class constructor) is that _Name will be visible within the class itself. There's a risk that the class will internally reference _Name rather than Name, everything will work fine, and at some later point in time you'll add some logic to Name that will not be called because you're using _Name within the class.

Example:

private string _Name = "Default Name";
public string Name
{
   get { return _Name.Left(42); }  // Changed the getter
   set { _Name = value; }
}

void MyOtherMethod()
{
   string foo = _Name; // Referencing the private field accidentally instead of the public property.
   // Do something with foo
}


Basic behavior and purpose of both of the property method is almost same. But the major difference is in the implementation. The difference between

public string Name{get;set;}

AND

 private string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name=value;  }
        }

is if you use short property syntax (introduced in framework 3.0 or later), then the property sting is never initialized i.e. if you directly use "Name" property anywhere without setting the value to it, it will return a NULL value. But if you use second syntax to initialize the property value, it will return a EMPTY string because when you initialize a string, it is initialized with a EMPTY value not the NULL. So if you return the property value without initializing using FULL Property Method, it will always return the EMPTY string not the NULL value.


I dont think there is any difference in compiled code. The reason why you may want to do the full way though is if you want to add a default value (which can be done in the constructor in short hand form), or add additional code to the getter or setter

EDIT: Actually, your code is wrong it should be

private string _Name;
public string Name
{
   get { return _Name; }
   set { _Name = value; }//change here
}

not...

value = _Name;


One difference is that you can set a default on the private string when you do this

private string _Name = "Default Name";
public string Name
{
   get { return _Name; }
   set { value = _Name; }
}

Once compiled the two examples you showed are the same.


It is simply a short form, the underlying variable is still generated as a supporting backing field (where the data is stored,) but automatically - this is useful if you are literally just getting and setting and don't need any specific implementation details in either accessor.


For this particular implementation of second form, both are equivalent. Because the compiler will generate almost the same code if you simply write the first form.

That is, the compiler is going to add code to it:

public string Name{get;set;}

making it look like this:

private string generatedCode_Name;
public string Name
{
      get { return generatedCode_Name; }
      set { generatedCode_Name = value; }
}

By the way, this is incorrect

set { value = _Name; } //I believe its a typo!

I think you meant:

set {  _Name = value; }


For the example as written they are an exact equivalent.

Auto-implemented properties are syntactic sugar introduced to address exactly these type of situation, where the property is used just to avoid having a public field, with no extra logic in the getter/setter. However, an auto-implemented property gives you all the benefits of properties, including metadata. Here's a rather old but still relevant link that explains a little bit more about them.

Behind the scenes, the compiler generates a backing field very similar to your own.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜