If I have a property in a class defined as follows:
private int mSomeNumber;
public int SomeNumber
{
get
{
return mSomeNumber;
}
}
and inside the same class, I am curious if people use the member variable, or if you use the property. For example:
public void DoSomething()
{
if(mSomeNumber == 0) // This way?
//if(SomeNumber == 0) // Or this way?
{
// Do something
}
}
I’m thinking that using the member variable directly might save a call, but I’m wondering if the property will be compiled to the same thing. Does anyone know if it is or what the “standard” might be?
You should use the property, unless you have a specific requirement for a workaround (example as per Henk’s answer).
That way you can alter the functionality in the property getter without changing your calling code. For example, your getter might format the value for a prettier return value,
or increment it by a constant, or if it is just wrapping up state from else where (in a nested object), etc.