Define Constants In C# Language

Introduction

As the name suggested , constants pre define fixed value, which is the program may not alter during the execution. These fixed values are also called literals. The constants are treated just like regular variables accept their values cannot be modified after their definition.    Constant are define in program using the const keyword.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Define_constants_in_c_sarp

{

    class Program

    {

        static void Main(string[] args)

        {

            const double pai = 3.141;                   //Define Constant variable pai, which is type of double

            double radius;

            Console.WriteLine("Enter radius of circle");

            radius = Convert.ToDouble(Console.ReadLine());

            double area = pai * radius * radius;

            Console.WriteLine("Radius Of Circle:{0}\nArea of Circle:{1}\n",radius,area  );

         

 

        }

    }

}


Output:


output.jpg


Note: Constant and read only both are same but constant value do not change during execution and we have change read only value during execution through constructor. 


Next Recommended Reading Look at C# constants