Skip to content

Latest commit

 

History

History
71 lines (49 loc) · 3.28 KB

File metadata and controls

71 lines (49 loc) · 3.28 KB
description Declare compile time constants with the `const` keyword
title The const keyword
ms.date 01/21/2026
f1_keywords
const_CSharpKeyword
const
helpviewer_keywords
const keyword [C#]

The const keyword

Use the const keyword to declare a constant field or a local constant. Constant fields and locals aren't variables and can't be modified. Constants can be numbers, Boolean values, strings, or a null reference. Don't create a constant to represent information that you expect to change over time. For example, don't use a constant field to store the price of a service, a product version number, or the brand name of a company. These values can change over time, and because compilers propagate constants, other code compiled with your libraries needs to be recompiled to see the changes. See also the readonly keyword. For example:

const int X = 0;
public const double GravitationalConstant = 6.673e-11;
private const string ProductName = "Visual C#";

Interpolated strings can be constants if all expressions used are also constant strings. This feature can improve the code that builds constant strings:

const string Language = "C#";
const string Platform = ".NET";
const string FullProductName = $"{Platform} - Language: {Language}";

[!INCLUDEcsharp-version-note]

The type of a constant declaration specifies the type of the members that the declaration introduces. The initializer of a local constant or a constant field must be a constant expression that the compiler can implicitly convert to the target type.

A constant expression is an expression that the compiler can fully evaluate at compile time. Therefore, the only possible values for constants of reference types are strings and a null reference.

You can declare multiple constants in a single constant declaration, such as:

public const double X = 1.0, Y = 2.0, Z = 3.0;

The static modifier isn't allowed in a constant declaration.

A constant can participate in a constant expression, as follows:

public const int C1 = 5;
public const int C2 = C1 + 100;

Note

The readonly keyword differs from the const keyword. You can only initialize a const field at the declaration of the field. You can initialize a readonly field either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, although a const field is a compile-time constant, the readonly field can be used for run-time constants, as in this line: public static readonly uint l1 = (uint)DateTime.Now.Ticks;

Examples

:::code language="csharp" source="./snippets/csrefKeywordsModifiers.cs" id="5":::

The following example shows how to declare a local constant:

:::code language="csharp" source="./snippets/csrefKeywordsModifiers.cs" id="6":::

C# language specification

For more information, see the following sections of the C# language specification:

See also