A Constant is an variable, whose value does not change during execution, once assigned.

C# Constant

Constant variables are declared using the const keyword. These are immutable at compile time and do not change during program execution. C# supports constants for variables, but not for methods, properties, and events. Constants by default static.

accessmodifier const datatype constant_name=  value
  • access modifier is private, protected, public, internal, and default

  • const is a keyword in C#.

  • datatype is an Inbuilt or user-defined data types

  • constant_name is a name of a constant, Its name follows Pascal Casing’s naming convention

public const int Age = 25;

Multiple constants declared in a single using below

Each constant declaration is separated by a comma, and ends with a semicolon ;

public const int age = 35, salary=5000, id=1;

Same can be written using below

public const int age = 35;
public const int salary = 5000;
public const int id = 1;

Constants are also assigned with the result of expressions. The expression value is always computed at runtime.

public const int first = 12;
public const int second = 24;
public const int result = first+second ;// expression

Here is an example

using System;
public class Program
    public static void Main()
        const int Age = 25;

        Console.WriteLine(Age); // Printing Constant to Console
        const int Age = 25;

It throws a Compilation error (line 8, col 3): The left-hand side of an assignment must be a variable, property, or indexer

Naming Convention for Constants

Microsoft defined the following naming conventions for Constants. Generally, Constants are fields

  • Constant name should be PascalCase.

These are guidelines, not strict, But you can follow them as per your organization. Some uses Capital Case for all constants Names across modules.