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.
Literal Constants:
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.
Symbolic Constants:
Symbolic constants assign a name to a constant value. You declare a symbolic constant using the following syntax:
const type identifier = value;
The const keyword is followed by a type, an identifier, the assignment operator (=), and the value with which you'll initialize the constant.
This is similar to declaring a variable, except that you start with the keyword const and symbolic constants must be initialized. Once initialized, a symbolic constant cannot be altered.
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.
Pascal notation is just like Camel notation except that the first letter is also uppercase (FreezingPoint).
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
Enumerations provide a powerful alternative to literal or simple symbolic constants. An enumeration is a distinct value type, consisting of a set of named constants (called the enumerator list).
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.
Using 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.
Casting:
Objects of one type can be converted into objects of another type. This is called casting. Casting can be either implicit or explicit.
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 nearly impossible to write a C# program without creating strings. A string object holds a series of characters.
You declare a string variable using the string keyword much as you would create an instance of any type:
string myString;
You specify a string literal by enclosing it in double quotes:
"Hello World"
It is common to initialize a string variable that contains a string literal:
string myString = "Hello World";
Statements :
In C#, a complete program instruction is called a statement. Programs consist of sequences of C# statements, each of which must end with a semicolon (;).
Expressions:
Statements that evaluate to a value are called expressions. You may be surprised how many statements do evaluate to a value.
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.
The exception to this rule is that whitespace within a string is treated as literal; it is not ignored. If you write:
Console.WriteLine("Hello World")
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 INTRODUCTION
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
No comments:
Post a Comment