Why are we able to write
public int RetInt
{
get;set;
}
instead of
public int RetInt
{
get{return someInt;}set{someInt=value;}
}
What is the difference between the two?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This feature is called Auto implemented properties and introduced with C# 3.0
For your question
In your case, none. Since you are not doing anything while setting or retrieving the value, but suppose you have want to do some validation or want to perform other types of check then :
The above can’t be done with Auto implemented properties.
One other thing where you can see the difference is when initializing a custom class type property.
If you have list of
MyClassThen in case of Normal property, its backing field can be initialized/instantiated other than the constructor.
In case of Auto implemented property,
You can only initialize
SomeOtherListin constructor, you can’t do that at Field level.