A variable can declare as constant by using the C# keyword const. A variable declared as the constant should assign its value at its declaration time.
This value assigned to the C# constant variable is immutable (cannot change throughout the program’s execution life cycle) and called “compile-time”.
The const keyword applies to any built-in value type, enum, string literal, or reference type. Let us see an example of C# code using a constant variable.
using System; namespace CSharp_Tutorial { class Program { static void Main() { const double pi = 3.14159; Console.WriteLine("Pi value is {0} ", pi); } } }
In this C# example, pi is a double variable whose value is 3.14159.
Hence pi will declare as a const variable. The number remains the same throughout the program’s execution life cycle.