What is a Constant in C#?

What is Constant?

Today we will talk about constant. The constants are special variables. As we have seen the data types in our previous article, there are reference and value data types in C#. You must be thinking, what category constants belong to, reference type or value type. The right answer is a value type. The constants are value types of variables. In case you want to know more about them, you can click on the hyperlink above.

Why constants?

The variables are designed to save data in it and this data keeps on changing from time to time as per program or user requirements. Sometimes we need a variable that will store data that will never change. This is why Microsoft has provided you a solution to this problem in the form of constants.

Lets take an example

Support you are making software for a company that has 5000 employees. The core functionality of this software is to keep all types of records for all the employees, records like Salary, Medical history, leave management etcetera. The data about the employees will change like their salary, medical history but the company name will be the same for all the employees. This company name should be saved in a constant variable.

How to use Constants?

A constant can be of all types like String, Int, Double, Long etcetera. Because a constant should be able to save all types of data inside it. In case you need to save the value of PI or the speed of light in that case you will require a variable that can save numeric values. Below is the program you can see and understand how to declare a constant.

The constant variables get declared with a special keyword const. A constant variable has to be initialized at the time of declaration, this is a compulsion. Once a constant variable is initialized, its value can never be changed. If somebody tries to change it, the constant variable will through an exception. This makes the software more robust.

using System;

namespace Namespace
{
    class Program
    {
        
        static void Main(string[] args)
        {
            const double PI = 3.14d;

            Console.WriteLine("The value of PI is + {0}", PI);
            Console.ReadLine();
           
        }
    }
}

Output of above program

Constant-Program-Output

As you can see in the above program, the const keyword is used first to tell the compiler it’s constant then the second keyword double is used to tell the compiler that a double type of data is going to be saved in it. Further, it has the name of the constant variable which is PI and the value 3.14 is assigned to the variable. This 3.14 value can never be changed after this point.

In case you are interested to know how to double data type work you can read this article of mine.

Constants in Short

  1. The value of the constant should be initialized at the time of declaration.
  2. Once a value is assigned to a constant will be unchanged throughout the program.
  3. You can declare constants for all data types in c#.
  4. The Constants are value types.

I hope you have understood the concept of constants and in case of any confusion, feel free to comment, I will try to make it clear for you again.

Stay safe, Stay healthy in this pandemic.

Cheers!