C# Const Example

Dot Net Perls
Const keyword

Constants never change. In the C# language, the const keyword encodes this restriction. This keyword describes an entity that cannot be changed at program runtime. Instead, it must be fully resolved at compile-time. Because of this restriction, const has advantages over variables.

Keywords

Magic numbers are literal numbers, such as 100 or 47524, that appear in the middle of a program without explanation. If you program in a language that supports named constants, use them instead. McConnell, p. 292

Example

Const is a reserved keyword. Essentially, it allows you to pull constant values such as strings or integral types into part of the program text that is easier for you to manage and edit. When you compile const values, the values are inserted into the parts of the program that use them, eliminating any variable lookup overhead. However, const declarations at the class level are retained in the assembly metadata.

Example that uses const [C#]

using System;

class Program
{
    const string _html = ".html";

    static void Main()
    {
	// This causes a compile-time error:
	// _html = "";

	Console.WriteLine(_html);         // Access constant
	Console.WriteLine(Program._html); // Access constant

	const string txt = ".txt";

	// This causes a compile-time error also:
	// txt = "";

	Console.WriteLine(txt);
    }
}

Output

.html
.html
.txt

Compile-time errors. The program contains two assignment statements that are commented out with single-line comments. They result in a compile-time error when you try to compile them. Compile-time errors are separate from exceptions and are raised before you can ever execute the program.

Accessing const values. Although you cannot assign const identifiers after compile-time, you can access them as much as you want. The program above accesses the _html constant string in two different ways. You can read a constant in the same way as a static variable. You can also describe the constant with the public accessibility modifier to use it throughout your program.

Errors

This is a warning

When refactoring your program, you may run into some errors related to constant fields. The following examples shows the compile-time error that will occur if you try to assign constants at runtime after they are already assigned. To fix this error, either modify the constant to make it a variable, or remove the assignment.

Compile-Time Error

Error 1 The left-hand side of an assignment must be a variable, property or indexer

Advantages

Programming tip

Constant fields have advantages in C# programs. They promote the integrity of your programs by telling the compiler not to allow you to change the values during runtime, which can result in fewer accidental bugs. Additionally, they improve performance over regular variables in many cases, particularly when using integral types such as int.

Int Type Readonly Fields

Summary

The C# programming language

We looked at an example of using the const reserved word in the C# programming language. The const keyword allows you to specify that a value is invariant and must not be modified after compile-time. This article showed an example of using the const modifier, and also discussed issues related to errors and accessing the const values. We saw local consts and class-level const strings.

Static Modifier