C# Constants

  • C# constants are immutable values which means they cannot be changed in a program once defined.
  • C# constants exist in the source code and can be recognized at compile time only.
  • C# constants will be replaced with its value at compile time and do not exist in EXE or DLL files.
  • C# constants are declared with const keyword and must be initialized immediately.
  • The constants can only be used in C# built-in data type excluding System.Objects.
  • C# constants can be initialized by another defined constants but circular reference is not allowed.

Syntax: [Access modifier] will be introduced after C# Class section.

[Access modifier] const <data type> <constant name> = value;

Examples and explanations in each line:

const int DaysPerYear = 365;                   // Legal, it defines an int constant.
const int DaysPerYear;                         // Illegal, a value must be assigned to the constant while declaring.
int const DaysPerYear = 365;                   // Illegal, the data type cannot be in the front of the keyword const.
const string Mode = "Debug";                   // Legal, define a string constant.
const int DaysInLeapYear = DaysPerYear + 1;    // Legal, DaysInLeapYear is defined by another constant DaysPerYear.
const DateTime DateOfBirth = DateTime.Now;     // Illegal, DateTime is not C# built-in data type.

The following example will calcuate the perimeter and area of a circle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;

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

            double r = 50;
            Console.WriteLine("The perimeter of the circle is {0}", 2*PI*r);
            Console.WriteLine("The area of the circle is {0}", PI*r*r);

            Console.Read();

        }
    }
}

Output

The perimeter of the circle is 314
The area of the circle is 7850
  • Line 9: Declare a double constant PI and initialized as 3.14.
  • Line 11: Declare a double variable r which stands for radius and give it a value 50.
  • Line 12: Calculate the perimeter of the circle and output the result. The formula is 2 * PI * r.
  • Line 13: Calculate the area of the circle and output the result. The formula is PI * r * r.

If I want to make the result more precisely, I will change the value in line 9 to 3.1416. Then the result will be the following.

The perimeter of the circle is 314.16
The area of the circle is 7854

Check the example below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;

namespace CalculateRate
{
    class Program
    {
        static void Main(string[] args)
        {
            const int HoursPerDay = 8;
            const int HoursPerWeek = HoursPerDay * 5;
            const int HoursPerYear = HoursPerWeek * 52;

            int rate = 40;
            //HoursPerYear = 365*8;
            Console.WriteLine("Weekly Pay:{0:C}", HoursPerWeek*rate);
            Console.WriteLine("Yearly Pay:{0:C}", HoursPerYear*rate);

            Console.Read();
        }
    }
}

Output

Weekly Pay:$1,600.00
Yearly Pay:$83,200.00

  • Line 9: Declare an int constant HoursPerDay.
  • Line 10-11: Declare 2 int constants and initialized by the other constant.
  • Line 13: Declare an int variable rate and assigned a value.
  • Line 14: Commented out because the constant cannot be changed any more after declaration.
  • Line 15-16: Output weekly pay and yearly pay. {0:C} is used to output the result in currency format.