A static
readonly
field is useful when a symbolic name for a constant value is desired, but when the type of the value is not permitted in a const
declaration or when the value cannot be computed at compile-time by a constant-expression. In the example
public class Color { public static readonly Color Black = new Color(0, 0, 0); public static readonly Color White = new Color(255, 255, 255); public static readonly Color Red = new Color(255, 0, 0); public static readonly Color Green = new Color(0, 255, 0); public static readonly Color Blue = new Color(0, 0, 255); private byte red, green, blue; public Color(byte r, byte g, byte b) { red = r; green = g; blue = b; } }
the Black
, Write
, Red
, Green
, and Blue
members cannot be declared as const
members because their values cannot be computed at compile-time. However, declaring the members as static
readonly
fields has much the same effect.