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.
const double Pi = 3.14159; const int SecondsPerDay = 24 * 60 * 60;
Try it
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.
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
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 asType.Name. - Value is baked into callers. If library A defines
public const int Max = 10and 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 usestatic readonlyor a property. - Allowed types. the integral types (
sbytethroughulong),char,float,double,decimal,bool,string, enums, and any reference type set tonull.DateTime,TimeSpanand other structs are not allowed; usestatic readonlyfor 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
caselabels and in constant patterns such asx is Max. - Default parameters. Optional parameter defaults must be compile-time constants, so
void F(int n = Limits.Max)works only ifMaxis const. - Naming. The convention is PascalCase (
MaxItems), not upper snake case.
Related
- readonly
- static
- default
- Tutorial: Constants and readonly, Variables