Define constants with const keywords : const « Class « C# / CSharp Tutorial






  1. The const modifier is used to declare fields or local variables that cannot be changed.
  2. These variables must be given initial values when they are declared.
  3. const implies static.
using System;

class Constants
{
    public const int value1 = 33;
    public const string value2 = "Hello";
}
class MainClass
{
    public static void Main()
    {
        Console.WriteLine("{0} {1}", 
        Constants.value1, 
        Constants.value2);
    }
}
33 Hello








7.35.const
7.35.1.Define constants with const keywords
7.35.2.Local Constants
7.35.3.The use of 'const int'
7.35.4.Use expressions to calculate and display the circumference of a circle