This documentation is archived and is not being maintained.


The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable cannot be modified. A constant declaration introduces one or more constants of a given type. The declaration takes the form:

[attributes] [modifiers] const type declarators;


attributes (optional)
Optional declarative information. For more information on attributes and attribute classes, see C# Attributes.
modifiers (optional)
Optional modifiers that include the new modifier and one of the four access modifiers.
One of the types: byte, char, short, int, long, float, double, decimal, bool, string, an enum type, or a reference type.
A comma-separated list of declarators. A declarator takes the form:

identifier = constant-expression

The attributes and modifiers apply to all of the members declared by the constant declaration.

The type of a constant declaration specifies the type of the members introduced by the declaration. A constant expression must yield a value of the target type, or of a type that can be implicitly converted to the target type.

A constant expression is an expression that can be fully evaluated at compile time. Therefore, the only possible values for constants of reference types are string and null.


The constant declaration can declare multiple constants, for example:

public const double x = 1.0, y = 2.0, z = 3.0;

The static modifier is not allowed in a constant declaration.

A constant can participate in a constant expression, for example:

public const int c1 = 5.0;
public const int c2 = c1 + 100;


// const_keyword.cs
// Constants
using System;
public class ConstTest 
   class MyClass 
      public int x;
      public int y;
      public const int c1 = 5;
      public const int c2 = c1 + 5;

      public MyClass(int p1, int p2) 
         x = p1; 
         y = p2;

   public static void Main() 
      MyClass mC = new MyClass(11, 22);   
      Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);
      Console.WriteLine("c1 = {0}, c2 = {1}", MyClass.c1, MyClass.c2 );


x = 11, y = 22
c1 = 5, c2 = 10


This example demonstrates using constants as local variables.

// const_keyword2.cs
using System;
public class TestClass 
   public static void Main() 
      const int c = 707;
      Console.WriteLine("My local constant = {0}", c);


My local constant = 707
Note   The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants, as in the following example:
public static readonly uint l1 = (uint) DateTime.Now.Ticks;

See Also

C# Keywords | Modifiers