C# Constants
A constant is an identifier used in place of a value. (You can think of a constant as a variable whose value cannot be changed.) Constants should be used for fixed values that occur many places in your code. They should also be used in place of a value whose purpose is not immediately clear from the value.
Constants used in this way—also called symbolic constants—are declared in C# using the const keyword followed by the constant’s type, an identifier, and the assignment of value. A constant must be initialized when it is declared, and once it has been initialized the value cannot be changed.
For example, here are the declarations for three notional constants:
const int maxSearchRecords = 41928;
const decimal interestRate = 6.75M;
const string companyMotto = "Best of the Best of the Best!";
The first of these, maxSearchRecords, might represent the maximum number of records in a search routine. Even if you only use this value only once, using an identifier in this manner makes it much clearer what it represents than would be the case if the value itself was used. It is also a good idea to put constants you may need to change in a place where you can find them—at the top of a procedure, or in their own class module, as appropriate.
The second constant, interestRate, might be used in a financial application. The M following the value tells the compiler that the value is of type decimal rather than double. Assigning 6.75, which alone would be a literal of type double, to a decimal-type constant would produce a conversion error. (The trailing letter here can be upper- or lowercase: m or M.)
If the interestRate value is used repeatedly in the program, it’s certainly easier—and a better practice—to only change it once, when the constant value is assigned, rather than throughout the application. (Of course, in the real world you’d probably want to do this technique one better and provide a way to change the interest rate used without having to edit—and recompile—code.)
The final constant example, companyMotto, is included primarily to show that constants can contain string values, as well as other data types. Here’s companyMotto used with a MessageBox.Show method:
MessageBox.Show(companyMotto, "Sir!", MessageBoxButtons.OK);
The same reasons for using constants rather than literal values apply to string types, and perhaps companyMotto is an example of this: You don’t have to retype the string in numerous places in your application, and if the company changes its motto, you only have to change it in one place.
Enumeration Constants
Enumerations are lists of named constants—called enumeration constants—of the same type. (The list of constants within an enumeration is also called the enumerator list.)
Built-In Enumerations
As you start to use C# and the .NET Framework, you’ll almost certainly use the enumeration constants that are part of the .NET Framework’s pre-built types.
The fourth MessageBox.Show method argument represents the icon that will be displayed in the message box. This icon is selected by choosing from a list of enumeration constants, each a member of the MessageBoxIcon enumeration. You’ll see the list of enumeration constants for the MessageBox enumeration supplied by the auto-completion feature of the Code Editor when you type in a MessageBox.Show.
You can also find member information for an enumerator list in the Object Browser.
The icon that represents the choice made by the selection of the enumeration constant from the list of MessageBoxIcon members will be displayed when the message box is displayed:
const string companyMotto = "Best of the Best of the Best!";
MessageBox.Show(companyMotto, "Sir!", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
Custom Enumerations
Enumerations are too good to just use with types provided by .NET—you’ll want to use your own enumerations, using the enum keyword, in situation where related items form a natural list of possibilities.
An enumeration has a base type, which is the underlying C# intrinsic type for items in the enumeration. If the base type of an enumeration is not specified, it defaults to int. In addition, you needn’t assign a value to each enumeration constant (but can if you wish). In the default case, the first item in the enumerator list has a value of zero, and each successive enumerator is increased by one. For example, if you created an enumeration named toys with three items:
enum toys {train, dinosaur, truck};
the items would have values as follows:
|
Enumerator | Value |
| toys.train | 0 |
| toys.dinosaur | 1 |
| toys.truck | 2 |
Now, when you attempt to use this enumeration, the members appear alphabetically in the Code Editor’s auto-completion list, just like a built-in enumeration.
The members of the toys enumeration will also appear in the Object Browser.
By the way, if you attempted to display the value of toys.truck using a statement along the lines of
MessageBox.Show(toys.truck.ToString());
you’d get the string “truck” rather than the underlying value.
To access the underlying value of the enumeration constant, you would have to cast it to integer type. For example, under this scenario,
int x = (int) toys.truck;
the variable x now has a value of 2.
If you wish to explicitly assign values to the enumeration items, you can do so in the enum declaration. For example:
enum toys {train = 12, dinosaur = 35, truck = 42};
You can also start an enumeration list at whatever value you’d like, and only assign values to some items—in which case the default is to increment by 1. So, if the toys enumeration were modified as follows:
enum toys {train = 12,
dinosaur,
excavator = 41,
truck
};
then toys.dinosaur would have a value of 13 and toys.truck a value of 42.
|