tags:

views:

127

answers:

4

Snippets:

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

and

public double MemberVal   
 {    
  get; set;  
 }
+13  A: 

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

Matt
it was mistake. MemberVal is public.
Samvel Siradeghyan
@Samvel - Based on your edit, they are now the same. The first snippet will use one more variable than the second snippet, but the behavior is the same.
Matt
@Matt: "The first snippet will use one more variable than the second snippet" . . . ish . . . The class still have a private storage member for the property, it's autogenerated and invisible but can be seen through refelection and/or in IL.
Binary Worrier
+3  A: 

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.

driis
+5  A: 

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.

Binary Worrier
Except that you can access the field from code in the first case but not in the latter :-)
Joey
Except for the inaccessible, auto-generated private member in the second example.
ck
There was mistake. MemberVal is public.
Samvel Siradeghyan
@Johannes Rössel: Yes, that's well worth pointing out, have updated to include same :)
Binary Worrier
A: 
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.

JMSA