Possible Duplicate:
What is the best way to access properties from the same class, via accessors or directly?
I am still learning C#. Anyway I have a simple question but I am unsure the normal way to do this. Say I have the following in a class
private int _questionNo;
public int QuestionNo
{
get
{
return _questionNo;
}
private set
{
_questionNo = value;
PropChanged("QuestionNo");
}
}
If I want to set the property within the class itself should I use
_questionNo = number;
or
QuestionNo = number;
You should always use the Public getter-setter, and should NEVER touch the private backing field away from inside the Getter-Setter, unless of course you don’t want the events inside of the Setter to occur.
The reason for this is that if you ever need to change something in the Setter that you want to happen you won’t have to change all your variables.