My C# Handout
My C# Handout
My C# Handout
Keywords
Keywords are the character string tokens used to define the C# language. Table 3-2 gives a complete list of
the C# keywords.
Some important things to know about keywords are the following:
• Keywords cannot be used as variable names or any other form of identifier, unless
prefaced with the @ character.
• All C# keywords consist entirely of lowercase letters. (.NET type names, however, use
Pascal casing.)
Contextual keywords are identifiers that act as keywords only in certain language constructs. In those
positions, they have particular meanings; but unlike keywords, which cannot ever be used as identifiers,
contextual keywords can be used as identifiers in other parts of the code. Table 3-3 lists the contextual
keywords.
27
Chapter 3 ■ Overview of C# Programming
(continued)
38
Chapter 3 ■ Overview of C# Programming
Table 3-4. (continued)
Name and Characters Meaning
Number Similar to fixed-point representation but includes comma or period separators
N, n between each group of three digits, starting at the decimal point and going left.
Whether it uses a comma or a period depends on the culture setting of the PC
running the program.
Precision specifier: The number of decimal places.
Sample: Console.WriteLine("{0 :N2}", 12345678.54321);
Output: 12,345,678.54
Percent A string that represents percent. The number is multiplied by 100.
P, p Precision specifier: The number of decimal places.
Sample: Console.WriteLine("{0 :P2}", 0.1221897);
Output: 12.22 %
Round-trip The output string is chosen so that if the string is converted back to a numeric
R, r value using a Parse method, the result will be the original value. Parse methods
are described in Chapter 27.
Precision specifier: Ignored.
Sample: Console.WriteLine("{0 :R}", 1234.21897);
Output: 1234.21897
Scientific Scientific notation with a mantissa and an exponent. The exponent is preceded
E, e by the letter E. The E character will be the same case as the specifier.
Case-sensitive Precision specifier: The number of decimal places.
Sample: Console.WriteLine("{0 :e4}", 12.3456789);
Output: 1.2346e+001
Both alignment specifiers and format specifiers continue to be available with string interpolation.
39
Chapter 4 ■ Types, Storage, and Variables
The nonsimple predefined types are somewhat more complex. Table 4-2 shows the predefined
nonsimple types.
48
Chapter 4 ■ Types, Storage, and Variables
User-Defined Types
Besides the 16 predefined types provided by C#, you can also create your own user-defined types. There are
six kinds of types you can create.
• class types
• struct types
• array types
• enum types
• delegate types
• interface types
You create a type using a type declaration, which includes the following information:
• The kind of type you are creating
• The name of the new type
• A declaration (name and specification) of each of the type’s members—except for
array and delegate types, which don’t have named members
Once you’ve declared a type, you can create and use objects of the type just as if they were predefined
types. Figure 4-5 summarizes the use of predefined and user-defined types. Using predefined types is a
one-step process in which you simply instantiate the objects of that type. Using user-defined types is a
two-step process. You must first declare the type and then instantiate objects of the type.
Figure 4-5. The predefined types require instantiation only. The user-defined types require two steps:
declaration and instantiation.
49
Chapter 5 ■ Classes: The Basics
class Program
{
static void Main()
{
// Create two instances of DaysTemp
DaysTemp t1 = new DaysTemp();
DaysTemp t2 = new DaysTemp();
75