C# Basics

C# Constants

The objective of this article is to familiarize you with the C# Constants. Compile-Time used with the const keyword and Run-Time constant with readonly modifier.

C# Constant (Compile-Time Constants)

Use the const keyword before the data type to declare a constant field or a constant local to a method. This modifier means that the field value cannot be changed after the initial assignment throughout the lifetime of the application, and it must, therefore, be assigned a value as it is declared—in other words, an immutable value. Any attempt to assign a new value to the constant will result in a compile-time error.

Note: A constant can be any of the built-in numeric types, bool, char, string, or an enum type.

C# Constant Declaration

The syntax for defining a constant is given below.

access_modifier const data_type name_of_constant = value;

access_modifier: public, internal, private, protected.
const: keyword.
data_type: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, enum, string.
name_of_constant: Identifier given to the constant.
value: Value that the constant will hold.

Sample code is given below.

using System;

class Program
{
  const int b = 5; // compile-time constant field
  static void Main(string[] args)
  {
    const int a = 10; // compile-time local constant
  }
}

The const modifier creates a compile-time constant, so the compiler will replace all usage of the constant with its value. Therefore, the value must be assigned at the time of declaration.

A field that is marked with const is accessed as if it was a static field.

int a = Program.b;

Notice that you are referencing the constant data defined by Program class using a class name prefix (i.e., Program.b). This is because the constant field is implicitly static by default, and that’s why the modifier static is not allowed in a declaration. Doing so will result in a compile-time error.

static const int b = 5;

Error: The constant ‘b’ cannot be marked static.

C# Constant Example

C# Constant (Compile-Time C# Constants)

Run Demo

C# Constant in Short

  • const keyword.
  • Accessed using “ClassName.VariableName”.
  • Applied to fields and local variables.
  • Requirement to initialize a value in design time which cannot be changed.
  • Initialized with a value at the time of declaration only.
  • Implicitly static by default. Can’t be marked with static keyword.
  • Evaluated at compile-time.
  • Allowed value types (built-in) are sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, and all enum types. Other value types such as TimeSpan or Guid will fail at compile-time.
  • Allowed reference types are string and a null reference.

C# Readonly (Run-Time Constants)

Another modifier similar to const is readonly, which creates a run-time constant. This modifier may only be applied to fields and can only be initialized either at the declaration or in a constructor but nowhere else. However, once it has been assigned a value, it can’t be modified. Therefore, readonly fields can have different values depending on the constructor used.

When you want to declare reference type constants that cannot be calculated during compilation of the program, you must use the combination static readonly instead of the const modifier.

C# Readonly Declaration

The syntax for defining a readonly is given below.

access_modifier static readonly reference_type name_of_readonly;

static, readonly : keywords
reference-type: A type, value of which cannot be calculated at compilation time.

Readonly fields are not implicitly static. Thus, if you want to expose the field from the class level, you must explicitly use the static keyword if you know the value of a static readonly field at compile time. Here’s an example of some constants:

C# Readonly Example

C# Readonly (Run-Time C# Constants)

Run Demo

C# Readonly in Short

  • C# readonly keyword.
  • Accessed using “InstanceName.VariableName”. Use static keyword to make it class member.
  • Applied to fields only.
  • Requirement to initialize a value in run-time which cannot be changed.
  • Initialized with a value at the time of declaration or in a constructor.
  • Non static member by default.
  • Evaluated at run-time.
  • Applied to any data type.
  • Often used when injecting dependencies.

C# Constant Naming

Follow the PascalCase rule according to Microsoft’s official C# coding convention.

Here are some examples of correctly named constants:
Pi, PathSeparator, BigCoffee, SpeedOfLight

Sometimes, ALL-CAPS is used, but it is not officially supported by the Microsoft code conventions, even though it is widely distributed in programming:
E, PI, FONT_SIZE_IN_POINTS, THE_ANSWER


C# Reference | Microsoft Docs