开发者

Are this 2 code parts same?

Snippets:

 private double memberVal;   
 public double MemberVal   
 {   
  get { return memberVal; }   
  set { memberVal= value; }   
 }

and

pu开发者_如何学Cblic double MemberVal   
 {    
  get; set;  
 }


Almost. In the second example, MemberVal is not publicly accessible.


No, but now they are the same

private double memberVal;
public double MemberVal
{
    get { return memberVal; }
    set { memberVal= value; }
} 

and

public double MemberVal
{
get; set;
}

Update Except - as pointed out by Johannes Rössel - that you can access the field from code in the first case but not in the latter :-) –

Meaning that in the first code sample, within your class you can directly set the backing member for the property (i.e. private double memberVal1 e.g. memberVal = 1.1;), where in the second, there is still a private backing member for the property, but it's now invisible.
You can only access it through the property.


Yes, that code is equivalent, apart from MemberVal not being public in the second example (did you mean that). In the latter case, the compiler generates a field for you. It will have another, auto-generated name.


private double memberVal;
public double MemberVal
{
    get { return memberVal; }
    set { memberVal= value; }
} 

public double MemberVal
{
    get; set;
}

second of the code-snippets is not supposed work on .net 2.0, coz it was introduced in .net 3.0.

The second is the short-hand notation for the first one but works only on .net 3.0 or higher.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜