C# – Should Fields or Properties Be Used?

cproperties

I've seen some people who make properties for every single member, private or not… does this make any sense?

private string mWhatever;

private string Whatever
{
    get
    {
        return this.mWhatever;
    }
    set
    {
        this.mWhatever = value;
    }
}

I could see it making sense in 1% of the cases at times when you want to control access to the member inside the class containing it.
Because if you don't use properties for every member it would lead to inconsistencies and checking to see if the member has an access or not (since you have access to both in the scope of the class).

Best Answer

Short answer: Yes, when there is a need. Otherwise, use an Auto-Implemented Property getter and setter like private string Whatever { get; set;}

  • It is very handy When you are using a close domain approach
  • It is also handy when a specific logic should be checked when you are setting the value

Here is a full description of when you would be using private setters: C# property usage.