Are these 2 pieces of code the same?
4 answers
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 Also, as Johannes Rössel pointed out, you can access the field from code in the first case, but not in the last :-) -
Assuming that in the first code example inside your class, you can directly set the backing element for the property (i.e. private double memberVal1
eg memberVal = 1.1;
), where in the second, there is still a private backing element for the property, but this is now invisible. You can only access it through a property.
+5
a source to share
private double memberVal;
public double MemberVal
{
get { return memberVal; }
set { memberVal= value; }
}
public double MemberVal
{
get; set;
}
the second code snippet is not supposed to work with .net 2.0, but it was introduced in .net 3.0.
The second is a short notation for the first, but only works on .net 3.0 or higher.
0
a source to share