So if you use the set to set the private field when you return it using get you will get whatever it was set to. Or if you set the private field directly when you return it via the property get you will get whatever it was set to. For example
private String _test = "Test";
public String Test
{
get{return _test;}
set{_test = value;}
}
String newString = Test;
//newString value will be "Test" as that is what the get returned
Test = "testSet";
//_test is now "testSet";
_test = "directlySet";
//_test is now "directlySet" and the Test get will return "directlySet"
So your question about ambiguity is as long as your get returns the correct private field then the field and property will remain the same.
If you do not declare method bodies for get or set
public String Test
{
get;
set;
}
then it will be the same as if you declared a private field as the compiler will generate one for you using the default constructor, you just will not be able to access the field directly
edit:
Also you are being told that set has no method body because you have given get one, so therefore it won't generate a method for the set. If you define get you either have to define a set or not have a set at all and vice versa.
You seem to be thinking of the property as its own field as well. The property holds no value, when you call the property it calls the get method, it does not remember the result, when you assign to it the set method is called, it does not remember the result. So calling the get method doesn't "update the property" it just returns whatever the get method says to return.
This post has been edited by bonyjoe: 29 April 2012 - 06:09 AM

New Topic/Question
Reply




MultiQuote








|