I'm a little confused about accessor's in C#. I assumed something like this could be done for private accessor's:
private string m_name =
{
get { return m_name; } // Not sure if this is actually correct. Maybe use 'this'
set { m_name = value } // Not even sure if this is correct
}
I'm not sure if the code above is valid. I've not used accessors in C#.
Instead, documentation states to do this:
class Employee
{
private string m_name;
public string Name
{
get { return m_name; }
protected set { m_name = value; }
}
}
Why is this done, because from my perspective the user can still access the private m_name property via Name. Doesn't this defeat the point of private (or even protected) properties?
In the first example shouldn't the compiler know its private and thus create the methods behind the scenes (as I believe it does at compile time)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…