Why Use Constants?
A constant is similar to a variable as it defines a name for a value. However, a constant differs from a variable because once defined, the value assigned to the constant cannot be changed. The benefit of constants is their assistance in creating self-documenting code. They also allow you to declare key values in a single place, permitting easy maintenance should the value need to be updated and the software recompiled.
To demonstrate the self-documenting value of constants, consider the following line of code:
price += (price * 0.175) + 4.99;
You could probably work out what the developer was trying to do here. A price is being increased by 17.5% and then a further £4.99 is being added. However, if the code is rewritten using constants instead of magic numbers, the purpose becomes obvious.
price += (price * ValueAddedTax) + DeliveryCharge;
Not only is this second code sample easier to read, it allows the definitions of tax rules and delivery charges to be changed in a single location. The constants can be modified once and the new values automatically be updated throughout your code. NB: The scope of a constant is defined in a similar manner to variable scopes.
Declaring a Constant
Constants are declared using similar syntax to other variables but with the const prefix . Here is how the previous example code looks with the constant definitions included:
double price = 100;
const double ValueAddedTax = 0.175;
const double DeliveryCharge = 4.99;
price += (price * ValueAddedTax) + DeliveryCharge;
Constants must be possible to evaluate at compile-time. This limits the use of constants to values that can be expressed directly in the source code, such as numeric values and strings. Values that cannot be initialised without a constructor are not permitted, meaning reference type constants may only be null.
Enumerations
An enumeration, or enumerator list, declares a series of related constants, each with an integer value. Enumerations are useful for defining sequences and states, particularly when there is a natural progression through those states. This is because each constant in the list can be formatted and compared using either its name or value.
Defining an Enumeration
The simplest method for declaring an enumeration is to list all of the possible names in a set of braces after the enumeration's name. To indicate that an enumerator list is being defined, the enum keyword is used as a prefix to the name. In this case, the first item in the list is assigned the value zero and each subsequent item is incremented.
This simple declaration defines a constant for every month of the year:
enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }
Using Enumerated Values
Once an enumeration is declared within a class, all of the methods of the class are able to utilise the values of the list using the names of the list and of the individual item separated using the member access operator (.) The following example shows how a value from the list can be retrieved and outputted as either an integer value or as a string.
NB: Enumerations can be declared with wider scopes to allow their use in multiple classes.
class Program
{
enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }
static void Main(string[] args)
{
Console.WriteLine((int)Month.Feb); // Outputs "1"
Console.WriteLine(Month.Feb); // Outputs "Feb"
}
}
As the enumeration provides a list of linked numeric values, it is possible to perform simple operations on the integer representations. In the next sample, a variable is declared of the enumerated type. Mathematical operators are then used to modify the value. Note that when the range of the enumeration is exceeded, the values revert back to the integer representations.
class Program
{
enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }
static void Main(string[] args)
{
Month mon = Month.Jan;
for (int i = 0; i<=12; i++)
{
Console.WriteLine(mon++);
}
}
}
/* OUTPUT
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
12
*/
16 August 2007