CSharp - Type Creation Constants

Introduction

A constant is a static field whose value can never change.

A constant is evaluated at compile time.

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

A constant is declared with the const keyword and must be initialized with a value.

For example:

class Test
{
    public const string Message = "Hello World";
}

A constant is more restrictive than a static readonly field.

Constants can also be declared local to a method. For example:

static void Main()
{
   const double twoPI  = 2 * System.Math.PI;
   
}

Nonlocal constants allow the following modifiers:

Modifiers Value
Access modifiers public internal private protected
Inheritance modifier new

Related Topics