C# Const Example

You want to see an example of the const keyword and how it can be applied in the C# programming language. The const keyword describes an entity that cannot be changed at program runtime and must be fully resolved at compile-time. Because of this restriction, it has advantages over variables. Here we look at examples of using the const keyword to describe strings in the C# language, and then explore issues related to the const keyword in C# programs.

Using const string

First, const is a reserved word in the C# programming language. Essentially, is 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 of the program ===

.html
.html
.txt

Compile-time errors. The program text above 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.

Const errors

When refactoring your program, you will likely 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.

~~~ Const error (Visual Studio) ~~~

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

Const advantages

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.

See Readonly Field Usage.

Summary

Here 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.

See Static Overview.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen