Home
C#
const Example
Updated Feb 9, 2025
Dot Net Perls
Const. The C# const keyword indicates a constant. It describes an entity that cannot be changed at program runtime. Instead, the entity must be fully resolved at compile-time.
It is impossible to reassign a constant. With constants, we lose the ability to modify variables at runtime but we gain performance and compile-time validation.
Example. With const, we move constant values into a single part of the program. The values are inserted into the parts of the program that use them—there is no lookup overhead.
Part 1 Here we access the _html constant string in 2 ways. We can read a constant in the same way as a static field.
static
Part 2 A constant can be placed in the local scope. Other than scope, there is no difference between a local const and a global const.
Part 3 We cannot reassign consts in a program. This would result in a compile-time error.
using System; class Program { const string _html = ".html"; static void Main() { // Part 1: access constant. Console.WriteLine(_html); Console.WriteLine(Program._html); // Part 2: local constant. const string txt = ".txt"; Console.WriteLine(txt); // Part 3: invalid statements. // ... This causes a compile-time error. // _html = ""; // ... This causes a compile-time error also. // txt = ""; } }
.html .html .txt
Left-hand side error. This is what happens when we try to mutate a constant value. We get a C# compile-time error. This program won't go far in life.
class Program { static void Main() { const int value = 10; value = 20; } }
Error CS0131 The left-hand side of an assignment must be a variable, property or indexer
Constant error. All parts of a constant expression must themselves be constant. So we cannot buildup a const from variables. No variables are allowed.
Tip To fix this error, either modify the constant to make it a variable, or remove the assignment.
readonly
class Program { static void Main() { int size = 5; const int value = 10 + size; } }
Error CS0133 The expression being assigned to 'value' must be constant
Const, notes. Constants promote the integrity of programs by telling the compiler not to allow changes to the values during runtime. This should result in fewer accidental bugs.
Also Const may improve performance over variables in many cases, particularly when using types like int.
Summary. Const is a reserved word. It specifies that a value is invariant and must not be modified after compile-time. Const values, like const strings, simplify and optimize programs.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Feb 9, 2025 (simplify).
Home
Changes
© 2007-2025 Sam Allen