TheDeveloperBlog.com


C# Public Static Readonly Field

Public static readonly fields are recommended. They are used for types that cannot be represented as const values, but are not subject to change during execution. These fields can lead to code resistant to programmer errors.

Readonly

Example. This example program uses public static readonly fields. The language specification recommends using public static readonly fields when you cannot use a const field or when the field is subject to change in the future.

Const

Readonly fields carry no performance penalty over normal fields. But const values are somewhat faster—they are inserted directly into the locations where they are used. Add the System.Drawing assembly reference to compile this program.

Drawing
Class that uses public static readonly fields: C#

using System.Drawing;

static class Points
{
    //
    // Defines four public static readonly fields on a static class.
    //
    public static readonly Point TopLeft = new Point(0, 0);
    public static readonly Point TopRight = new Point(1, 0);
    public static readonly Point BottomRight = new Point(1, 1);
    public static readonly Point BottomLeft = new Point(0, 1);
    // public const Point TopLeft = new Point(0, 0);
}

Program.cs with Main method: C#

using System;

class Program
{
    static void Main()
    {
	//
	// Uses the public static readonly fields.
	//
	Console.WriteLine(Points.TopLeft);
	Console.WriteLine(Points.TopRight);
	Console.WriteLine(Points.BottomRight);
	Console.WriteLine(Points.BottomLeft);
	// Points.TopLeft = new System.Drawing.Point();
    }
}

Output

{X=0,Y=0}
{X=1,Y=0}
{X=1,Y=1}
{X=0,Y=1}

The program defines a static class called Points that stores four Point references that are readonly fields. They use the public accessibility modifier so you can access them anywhere in the source code in your project.

Point, PointF

Tip: With static fields, you can use the constructor for a type and it is only executed once.

In Main, the four Point references are loaded. The readonly Point fields will always have their constructors called before being used because of the order that static constructors are executed.

Static Constructor

Errors are encountered when trying to use a const class or struct, and also when trying to assign a readonly field to a value outside of a variable declarator or constructor. These are both compile-time errors issued by the C# compiler.

This protects you from deploying bad code. But it can result in headaches if you are unfamiliar with the language. If you encounter these errors, try to remove the readonly modifier, or remove the assignment itself.

Error 1:

A static readonly field cannot be assigned to
(except in a static constructor or a variable initializer)

Error 2:

The type 'System.Drawing.Point' cannot be declared const


Specification. The concept of using public static readonly fields to simulate constant class instances is shown in the C# specification itself. This technique is important enough to be emphasized in the primary reference material. It is effective.

Tip: I recommend you check out the annotated C# language specification for the original example. It uses the Color class.

Color

Summary. We looked at a specific way of using constant fields for types that cannot be represented with the const modifier. Specifically we saw how the C# specification offers the strategy of using public static readonly fields.