Search code examples
c#classfieldreadonly

What do we call unchangeable fields of a class?


I am writing down some notes on a C# course I'm currently taking, and I was wondering if there's a specifically referenced name to the fields in a class we create that should not be able to be changed. Do we simply call them, "Readonly Fields"? Take the following field (width) in the Map class as an example:

public class Map
    {
      public readonly int Width;
        public int Height;

        public Map(int width, int height)
        {
            Width = width;
            Height = height;
        }
    }

Solution

  • The word for an object, field or property whose value doesn't change for its entire lifetime is immutable.

    System.String is a classic example -- any time you need a new string value, you actually get an entirely new object.