Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The const keyword

Reference for C# const: compile-time constants, allowed types, constant expressions, const locals, versioning pitfalls and how const differs from readonly.

const declares a named value that the compiler knows at compile time and that can never change. The compiler replaces every use of the name with the value itself, so a constant costs nothing at run time and can be used where a compile-time value is required: case labels, attribute arguments, default parameter values and array sizes in some contexts.

A constant can be a field or a local variable. Its initializer must be a constant expression, which means literals and other constants combined with operators the compiler can evaluate.

C#
const double Pi = 3.14159;
const int SecondsPerDay = 24 * 60 * 60;

Try it

C# Constants in expressions and case labels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# What counts as a constant expression

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
const Type name = constantExpression [, name2 = constantExpression2];
Part Description
Type A built-in numeric type, bool, char, string, an enum, or any reference type with the value null. Structs, arrays and classes (other than string) cannot be constants.
constantExpression A literal, another constant, or an operator combination the compiler can evaluate, including + on strings, ?:, casts between constant types, default, sizeof of built-ins and nameof.

Errors: CS0133 when the initializer is not constant; CS0134 when a reference constant (other than string) is initialized with anything but null; CS0283 when the type cannot be a constant at all (const DateTime d = default;); assigning to a constant is CS0131.

Usage notes

  • Implicitly static. A const field belongs to the type, not to instances. You cannot write static const, and you access it as Type.Name.
  • Value is baked into callers. If library A defines public const int Max = 10 and application B uses it, B's compiled code contains the number 10. Change A to 20 and B keeps using 10 until you recompile B. For values that may change between releases use static readonly or a property.
  • Allowed types. the integral types (sbyte through ulong), char, float, double, decimal, bool, string, enums, and any reference type set to null. DateTime, TimeSpan and other structs are not allowed; use static readonly for them.
  • Const locals work like const fields but are scoped to the block. They are useful to name magic numbers inside one method.
  • String constants. + between constant strings is folded at compile time. Since C# 10, an interpolated string whose holes are all constant strings is a constant too. Holes of non-string type are not allowed.
  • Switch and patterns. Constants can appear in case labels and in constant patterns such as x is Max.
  • Default parameters. Optional parameter defaults must be compile-time constants, so void F(int n = Limits.Max) works only if Max is const.
  • Naming. The convention is PascalCase (MaxItems), not upper snake case.