C# Constant

A variable can declare as constant by using the C# keyword const. A variable declared as the constant should be assigned the value to it at its declaration time.

This value assigned to the constant variable is immutable (cannot be changed throughout the program’s execution life cycle) and called “compile-time”.

The const keyword applied to any built-in value types, enum, a string literal, or a 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);
        }
    }
}

OUTPUT

C# Constants

In this C# example,  pi is a double variable whose value is 3.14159.

Hence pi declared as a const variable. The value remains constant throughout the program’s execution life cycle.