What is Constants With Example Code

In C#, you can declare constant variables using the const keyword. Constants are variables whose values cannot be changed once they are assigned. Here's a program demonstrating constant variables



using System;
 
class Program
{
    static void Main(string[] args)
    {
        const int MaxValue = 100;   // Constant variable declaration
 
        // Trying to modify a constant variable will result in a compilation error
        // MaxValue = 200;  // This line will cause a compilation error
 
        Console.WriteLine($"Max Value: {MaxValue}");
 
        // Using constants in expressions
        const double data = 3.14159;
        double radius = 5.0;
        double area = data * radius * radius;
 
        Console.WriteLine($"Area of a circle with radius {radius} is {area}");
    }
}