Variables are a powerful tool, but there are times when you want to manipulate a defined value, one whose value you want to ensure remains constant. A constant is like a variable in that it can store a value. However, unlike a variable, you cannot change the value of a constant while the program runs.
For example, you might need to work with the Fahrenheit freezing and boiling points of water in a program simulating a chemistry experiment. Your program will be clearer if you name the variables that store these values FreezingPoint and BoilingPoint, but you do not want to permit their values to be changed while the program is executing. The solution is to use a constant. Constants come in three flavors: literals, symbolic constants, and enumerations.
A literal constant is just a value. For example, 32 is a literal constant. It does not have a name; it is just a literal value. And you can't make the value 32 represent any other value. The value of 32 is always 32. You can't assign a new value to 32, and you can't make 32 represent the value 99 no matter how hard you might try.
const type identifier = value;
Microsoft has promulgated white papers on how you should name the variables, constants, and other objects in your program. They define two types of naming conventions: Camel notation and Pascal notation.
In Camel notation, names begin with a lowercase letter. Multi-word names (such as "my button") are written with no spaces and no underscore and with each word after the first capitalized. Thus, the correct name for "my button" is myButton.
Microsoft suggests that variables be written with Camel notation and constants with Pascal notation. In later chapters, you'll learn that member variables are named using Camel notation, while methods and classes are named using Pascal notation.
These constants serve the same purpose as using the literal values 32 and 212 for the freezing and boiling points of water, respectively, in expressions that require them. However, because the constants have names, they convey far more meaning. Also, if you decide to switch this program to Celsius, you can reinitialize these constants at compile time to 0 and 100, respectively, and all the rest of the code should continue to work.
To prove to yourself that the constant cannot be reassigned, try un-commenting the last line of the preceding program, by removing the two slash marks.
BoilingPoint = 21;
When you recompile, you receive this error:
error CS0131: The left-hand side of an assignment must be
a variable, property or indexer
The base-type is the underlying type for the enumeration. You might specify that you are declaring constant ints, constant longs, etc. If you leave out this optional value (and often you will), it defaults to int, but you are free to use any of the integral types (e.g., ushort, long) except for char. For example, the following fragment declares an enumeration with unsigned integers (uint) as the base-type:
Notice that an enum declaration ends with the enumerator list, which contains the constant assignments for the enumeration, each separated by a comma.This programme rewrite above programme to use an enumeration.
You cannot just refer to FreezingPoint; instead, you use the enumeration identifier (Temperature) followed by the dot operator and then the enumerated constant (FreezingPoint). This is called qualifying the identifier FreezingPoint. Thus, to refer to the FreezingPoint, you use the full identifier Temperature.FreezingPoint.
You might want to display the value of an enumerated constant to the console, as in the following:
To make this work properly, you must cast the constant to its underlying type (int). When you cast a value you tell the compiler "I know that this value is really of the indicated type." In this case you are saying "treat this enumerated constant as an int." Since the underlying type is int, this is safe to do.
An implicit conversion happens automatically; the compiler takes care of it for you. If you have a short, and you assign it to a variable of type int, the compiler automatically (and silently) casts it for you. You don't have to take any action. This is safe, because an int variable can hold any value that might have been in a short variable.
Explicit conversions happen when you specifically cast a value to a different type by writing the new type in parentheses. The semantics of an explicit conversion are "Hey! Compiler! I know what I'm doing." This is sometimes called "hitting it with the big hammer" and can be very useful or very painful, depending on whether your thumb is in the way.
If you convert from int to short you can lose information. If the value in the int is greater than 32,767, it will be truncated in the conversion to a short.
the values in the two enumerated constants FreezingPoint and BoilingPoint are both cast to type integer; then that integer value is passed to WriteLine( ) and displayed.
Each constant in an enumeration corresponds to a numerical value. Inthat programme, each enumerated value is an integer. If you don't specifically set it otherwise, the enumeration begins at 0 and each subsequent value counts up from the previous.
It is common to initialize a string variable that contains a string literal:
string myString = "Hello World";
What happens in this statement is that the literal value 57 is assigned to the variable myVariable. The value of that assignment (57) is then assigned to the second variable, mySecondVariable. Thus, the value 57 is assigned to both variables. You can assign a value to any number of variables with one statement using the assignment operator (=), as in the following:
and the compiler will treat the two statements as identical. The key is to use whitespace to make the program more readable to the programmer; the compiler is indifferent.
each space between "Hello" and "World" is treated as another character in the string. (In this case there is only one space character.)
Problems arise only when you do not leave space between logical program elements that require it. For instance, although the expression:
The compiler knows that the whitespace on either side of the assignment operator is extra, but the whitespace between the type declaration int and the variable name myVariable is not extra; it is required.
This is not surprising; the whitespace allows the compiler to parse the keyword int rather than some unknown term intmyVariable. You are free to add as much or as little whitespace between int and myVariable as you care to, but there must be at least one whitespace character (typically a space or tab).
VISUAL STUDIO INTRODUCTION
C SHARP INTRODUCTION
C SHARP OUT LOOK
DOT NET AND C SHARP
C SHARP APPLICATION STRICTURE
OOPS AND C SHARP
IDE AND C SHARP
INSTANTIATING OBJECTS IN C SHARP
CLASSES AND OBJECTS IN C SHARP
OPERATORS IN C SHARP
SWITCH AND ITERATION IN C SHARP
BRANCHING IN C SHARP
CONSTANTS AND STRING
STATIC AND INSTANCE MEMBERS IN DOT NET