Readonly Fields : readonly « Class « C# / CSharp Tutorial






class Color
{
    public Color(int red, int green, int blue)
    {
        this.red = red;
        this.green = green;
        this.blue = blue;
    }
    
    int red;
    int green;
    int blue;
    
    public static readonly Color Red;
    public static readonly Color Green;
    public static readonly Color Blue;
    
    static Color()
    {
        Red = new Color(255, 0, 0);
        Green = new Color(0, 255, 0);
        Blue = new Color(0, 0, 255);
    }
}

class MainClass
{
    static void Main()
    {
        Color background = Color.Red;
    }
}








7.36.readonly
7.36.1.Use readonly
7.36.2.Readonly Fields
7.36.3.static readonly fields
7.36.4.Readonly and const
7.36.5.Creating a read-only property.