Constants

Variables are great for storing values, but sometimes you don't want a value to change, and as in C++, you can make such values into constants. Declaring a constant works just as declaring a variable, except that you also use the const keyword:

[attributes] [modifiers] const type declarators;

The parts of this statement are just the same as for variables; in fact, declaring a constant is like declaring a variable, but you just use the const keyword. Take a look at ch01_03.cs in Listing 1.3, where we're declaring a constant named pi and displaying its value.

Listing 1.3. Using a Constant (ch01_03.cs)
class ch01_03
{
  static void Main()
  {
    const float pi = 3.14159f;
    System.Console.WriteLine("Pi = {0}", pi);
  }
}

Here's what you see ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.