#337 – Declaring and Using Static readonly Fields

A readonly field in a class can be a static or an instance field.

A static readonly field is a field that has a single read-only value, regardless of the number of instances of the parent class.  Client classes can read from, but not write to, the field.  The field is initialized either as part of the declaration or within a static constructor.

        // Static readonly field, initialized at declaration time
        public static readonly string TheDogMotto = "Man's Best Friend";

        // Static readonly field, initialized in a constructor
        public static readonly uint NumberOfLegs;

        static Dog()
        {
            NumberOfLegs = 4;
        }

Other code can read these fields.

            string motto = Dog.TheDogMotto;
            uint numLegs = Dog.NumberOfLegs;