C#: Difference of Properties and Fields

This blog post is for me: to finally learn the difference between properties and fields in C#. That is something I don’t clearly know yet. To be honest I don’t know which is which and what are their difference in practice. I know there is two syntax to write them and I am using them both but that is all. Usually I just talk about properties no matter is it property or field. Now I will find out the difference and learn it by writing this blog post!

Fields

A field is a private member variable of a class. It can also be public or protected but it isn’t recommended. It is recommended to use properties in that case.

Below I have fields `_name`, `_age` and `_children`:

public class Human
{
  private readonly string _name;
  private int _age;
  private readonly List _children;
}

`readonly` keyword in `_name` and `_children` means that they can be set only in a constructor. It doesn’t mean that they are immutable. For example, following is OK for `_children` even if I add a value to it:

public void AddChild(Human child)
{
  _children.Add(child);
}

Mutable readonly fields: allowed or not?

Microsoft doesn’t recommend using `readonly` keyword with mutable types:

DO NOT assign instances of mutable types to readonly fields. – Field Design ( Microsoft Docs)

This was a new thing for me (worth to write this blog post!). But wait a moment! At least ReSharper recommends putting `readonly` to every field that isn’t set outside of the constructor. Is ReSharper or Microsoft wrong? I made a brief survey but couldn’t find a clear answer. Practically answer was “do what seems best to you”. Here are some references that are saying Microsoft’s recommendation isn’t correct:

I have used to use `readonly` as ReSharper recommends it and I like it. For me, it tells that this field can only be set in the constructor. But I can understand that it is confusing because `readonly`isn’t actually read-only. My recommendation is to do as your coding convention says about this.

Properties

Properties are public or protected accessors to fields. In many other languages, these would be setter and getter methods which you don’t have to define in C#, thanks to auto-properties. Practically quite often properties are used like public fields. In my case, most typical usage of properties is in (auto-generated) poco classes.

Here is an example of auto-property:

public class Human
{
  public string Name { get; private set; }
  public int Age { get; set; }
}

Here `Age` and `Name` can be used just like `_name` and `_age` in Fields chapter. The only difference is that they are public. `Age` is even publicly mutable but `Name` can be set only from inside of the class. But unlike `readonly` example in Fields chapter, this can be modified from anywhere from inside of the class.

You can also define custom getter and setter methods for properties:

public class Human
{
  private string _name;
  public string Name
  {
    get { return _name.ToUpper(); }
    set { _name = value; }
  }
}

Here I am using `_name` field and `Name` property references to it. Setter is equal to default setter. But getter returns name in upper case.

Conclusion

  • Field is a private member variable of a class.
  • Property is a public or protected accessor to the field.
    • You can define custom getter and setter for property or use auto-property.
  • Rule of thumb: if private it is a field, otherwise a property.

References and Further Resources

One thought on “C#: Difference of Properties and Fields

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s