01 January 2018

what is const keyword in c# language

First, let's understand the English dictionary meaning of the "constant" word. Constant means "a situation that does not change", "not changing or varying", etc. Now having understood the meaning of constant, now let's understand about constant in C# language, how to define const in C# and when to use them?, etc

How to define constants in C#

Constant in C# language are defined using "const" keyword as in the below example.

public const double PI = 3.1415926535897931;

The π (PI) value in Mathematics is a constant and so the PI is defined as a constant in .NET framework class Math.

Characteritics of constants in C#

  1. Constant fields value should be assigned value when they are declared. You cannot declare constant fields in one place and assign a value to it another place. If you don't assign a value to a const field during their declaration, you will get a compilation error.

    A const field requires a value to be provided

  2. As obvious as it looks Once constants are defined, you cannot change the value of them. If you try to assign value to them you will get compilation error.
  3. System.Math.PI = 3.14;

    The above line of code which is trying to set a new value for Math.PI constant will result will produce a compilation error.

    The left-hand side of an assignment must be a variable, property or indexer

  4. Constant fields can be declared with any of the access specifiers depending upon your need. Thre is no restriction as such.

    They can be public, private, protected, etc

  5. When you assign a constant value to a variable, such assignment value will be replaced by actual value.

    For exmple, let's consider a class.

    double radius = 6.5;
    double areaOfCircle = Math.PI * Math.Pow(radius, 2);

    In the above code post compilation the constant variable PI will get replaced with its actual value. So after compilation code will look like below:

    double radius = 6.5;
    double areaOfCircle = 3.14 * Math.Pow(radius, 2);

Const keyword usage example

Below C# program defines two classes. Result and ResultHelper. Result class has 3 decimal type constants defined. ResultHelepr class is using the constatnts and determining whether a result is pass/fail.

public class Result
        public const decimal DistinctionPercentage = 75;
        public const decimal FirstClassPercentage = 60;
        public const decimal PassPercentage = 35;

        public decimal Percentage { get; set; }

    public static class ResultsHelper
       public static void Display(Result result)
          if (result.Percentage >= Result.DistinctionPercentage)
                Console.WriteLine("Pass. Distinction Class!!!.");

         else if (result.Percentage >= Result.FirstClassPercentage)
                Console.WriteLine("Pass. First Class!!.");

         else if (result.Percentage >= Result.PassPercentage)

1 comment:

  1. Hello there, You've performed a fantastic job. I will definitely digg
    it and for my part suggest to my friends. I am sure they'll be benefited from this website.