C#idiomatic C#
constantscsharp/programming-guide/classes-and-structs

Language

Constants (C# Programming Guide)

Constants in C# are compile-time literal values, which do not change once the program is compiled. Only C# built-in types can be constants.

  • page csharp/programming-guide/classes-and-structs/constants
  • updated 07/20/2015

Constants are immutable values which are known at compile time and do not change for the life of the program. Constants are declared with the const modifier. Only the C# built-in types may be declared as const. Reference type constants other than System.String can only be initialized with a null value. User-defined types, including classes, structs, and arrays, cannot be const. Use the readonly modifier to create a class, struct, or array that is initialized one time at run time (for example in a constructor) and thereafter cannot be changed.

C# does not support const methods, properties, or events.

The enum type enables you to define named constants for integral built-in types (for example int, uint, long, and so on). For more information, see enum.

Constants must be initialized as they are declared. For example:

sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/constants/Calendar.cs1

csharp
//<Snippet1>
class Calendar1
{
    public const int Months = 12;
}
//</Snippet1>

//<Snippet2>
class Calendar2
{
    public const int Months = 12, Weeks = 52, Days = 365;
}
//</Snippet2>

//<Snippet3>
class Calendar3
{
    public const int Months = 12;
    public const int Weeks = 52;
    public const int Days = 365;

    public const double DaysPerWeek = (double) Days / (double) Weeks;
    public const double DaysPerMonth = (double) Days / (double) Months;
}
//</Snippet3>

class Calendar
{
    public const int Months = 12;

    static void Main()
    {
        //<Snippet4>
        int birthstones = Calendar.Months;
        //</Snippet4>

        Console.WriteLine(birthstones.ToString());
    }
}

In this example, the constant Months is always 12, and it cannot be changed even by the class itself. In fact, when the compiler encounters a constant identifier in C# source code (for example, Months), it substitutes the literal value directly into the intermediate language (IL) code that it produces. Because there is no variable address associated with a constant at run time, const fields cannot be passed by reference and cannot appear as an l-value in an expression.

Multiple constants of the same type can be declared at the same time, for example:

sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/constants/Calendar.cs2

csharp
//<Snippet1>
class Calendar1
{
    public const int Months = 12;
}
//</Snippet1>

//<Snippet2>
class Calendar2
{
    public const int Months = 12, Weeks = 52, Days = 365;
}
//</Snippet2>

//<Snippet3>
class Calendar3
{
    public const int Months = 12;
    public const int Weeks = 52;
    public const int Days = 365;

    public const double DaysPerWeek = (double) Days / (double) Weeks;
    public const double DaysPerMonth = (double) Days / (double) Months;
}
//</Snippet3>

class Calendar
{
    public const int Months = 12;

    static void Main()
    {
        //<Snippet4>
        int birthstones = Calendar.Months;
        //</Snippet4>

        Console.WriteLine(birthstones.ToString());
    }
}

The expression that is used to initialize a constant can refer to another constant if it does not create a circular reference. For example:

sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/constants/Calendar.cs3

csharp
//<Snippet1>
class Calendar1
{
    public const int Months = 12;
}
//</Snippet1>

//<Snippet2>
class Calendar2
{
    public const int Months = 12, Weeks = 52, Days = 365;
}
//</Snippet2>

//<Snippet3>
class Calendar3
{
    public const int Months = 12;
    public const int Weeks = 52;
    public const int Days = 365;

    public const double DaysPerWeek = (double) Days / (double) Weeks;
    public const double DaysPerMonth = (double) Days / (double) Months;
}
//</Snippet3>

class Calendar
{
    public const int Months = 12;

    static void Main()
    {
        //<Snippet4>
        int birthstones = Calendar.Months;
        //</Snippet4>

        Console.WriteLine(birthstones.ToString());
    }
}

Constants can be marked as public, private, protected, internal, protected internal or private protected. These access modifiers define how users of the class can access the constant. For more information, see Access Modifiers.

Constants are accessed as if they were static fields because the value of the constant is the same for all instances of the type. You do not use the static keyword to declare them. Expressions that are not in the class that defines the constant must use the class name, a period, and the name of the constant to access the constant. For example:

sample · csharpdocs/csharp/programming-guide/classes-and-structs/snippets/constants/Calendar.cs4

csharp
//<Snippet1>
class Calendar1
{
    public const int Months = 12;
}
//</Snippet1>

//<Snippet2>
class Calendar2
{
    public const int Months = 12, Weeks = 52, Days = 365;
}
//</Snippet2>

//<Snippet3>
class Calendar3
{
    public const int Months = 12;
    public const int Weeks = 52;
    public const int Days = 365;

    public const double DaysPerWeek = (double) Days / (double) Weeks;
    public const double DaysPerMonth = (double) Days / (double) Months;
}
//</Snippet3>

class Calendar
{
    public const int Months = 12;

    static void Main()
    {
        //<Snippet4>
        int birthstones = Calendar.Months;
        //</Snippet4>

        Console.WriteLine(birthstones.ToString());
    }
}

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See also