Sie sind auf Seite 1von 6

Chapter 3 ■ Overview of C# Programming

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.)

Table 3-2.  The C# Keywords

abstract const extern int out short typeof


as continue false interface override sizeof uint
base decimal finally internal params stackalloc ulong
bool default fixed is private static unchecked
break delegate float lock protected string unsafe
byte do for long public struct ushort
case double foreach namespace readonly switch using
catch else goto new ref this virtual
char enum if null return throw void
checked event implicit object sbyte true volatile
class explicit in operator sealed try when
while

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.

Table 3-3.  The C# Contextual Keywords

add ascending async await by descending dynamic


equals from get global group in into
join let on orderby partial remove select
set value var where yield

27
Chapter 3 ■ Overview of C# Programming

Standard Numeric Format Specifiers


Table 3-4 summarizes the nine standard numeric format specifiers. The first column lists the name of the
specifier followed by the specifier characters. If the specifier characters have different output depending on
their case, they are marked case-sensitive.

Table 3-4.  Standard Numeric Format Specifiers

Name and Characters Meaning


Currency Formats the value as a currency, using a currency symbol. The currency symbol
C, c used will depend on the culture setting of the PC running the program.
Precision specifier: The number of decimal places.
Sample: Console.WriteLine("{0 :C}", 12.5);
Output: $12.50
Decimal A string of decimal digits, with a negative sign, if appropriate. Can be used only
D, d with integer types.
Precision specifier: The minimum number of digits to use in the output string.
If the number has fewer digits, it will be padded with 0s on the left.
Sample: Console.WriteLine("{0 :D4}", 12);
Output: 0012
Fixed-point A string of decimal digits with a decimal point. Can also include a negative sign,
F, f if appropriate.
Precision specifier: The number of decimal places.
Sample: Console.WriteLine("{0 :F4}", 12.3456789);
Output: 12.3457
General A compact fixed-point representation or a scientific notation representation,
G, g depending on the value. This is the default, if no specifier is listed.
Precision specifier: Depends on the value.
Sample: Console.WriteLine("{0 :G4}", 12.3456789);
Output: 12.35
Hexadecimal A string of hexadecimal digits. The hex digits A through F will match the case of
X, x the specifier.
Case sensitive Precision specifier: The minimum number of digits to use in the output string.
If the number has fewer digits, it will be padded with 0s on the left.

Sample: Console.WriteLine("{0 :x}", 180026);


Output: 2bf3a

(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.

double var1 = 3.14159;


System.Console.WriteLine($"The value of var1 is {var1,10:f5}");

39
Chapter 4 ■ Types, Storage, and Variables

More About the Predefined Types


All the predefined types are mapped directly to underlying .NET types. The C# type names are simply aliases
for the .NET types, so using the .NET names works fine syntactically, although this is discouraged. Within a
C# program, you should use the C# names rather than the .NET names.
The predefined simple types represent a single item of data. They’re listed in Table 4-1, along with the
ranges of values they can represent and the underlying .NET types to which they map.

Table 4-1.  The Predefined Simple Types

Name Meaning Range .NET Framework Default


Type Value
sbyte 8-bit signed integer -128 to 127 System.SByte 0
byte 8-bit unsigned integer 0 to 255 System.Byte 0
short 16-bit signed integer -32,768 to 32,767 System.Int16 0
ushort 16-bit unsigned integer 0 to 65,535 System.UInt16 0
int 32-bit signed integer -2,147,483,648 to 2,147,483,647 System.Int32 0
uint 32-bit unsigned integer 0 to 4,294,967,295 System.UInt32 0
long 64-bit signed integer -9,223,372,036,854,775,808 to System.Int64 0
9,223,372,036,854,775,807
ulong 64-bit unsigned integer 0 to 18,446,744,073,709,551,615 System.UInt64 0
float Single-precision float 1.5×10-45 to 3.4×1038 System.Single 0.0f
double Double-precision float 5×10-324 to 1.7×10308 System.Double 0.0d
bool Boolean true, false System.Boolean false
char Unicode character U+0000 to U+ffff System.Char \x0000
decimal Decimal value with ±1.0×1028 to ±7.9×1028 System.Decimal 0m
28-significant-digit
precision

The nonsimple predefined types are somewhat more complex. Table 4-2 shows the predefined
nonsimple types.

Table 4-2.  The Predefined Nonsimple Types

Name Meaning .NET Framework Type


object The base class from which all other types, including the System.Object
simple types are derived
string A sequence of zero or more Unicode characters System.String
dynamic A type designed to be used with assemblies written in No corresponding .NET type
dynamic languages

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

Putting It All Together


The following code creates two instances and stores their references in variables named t1 and t2.
Figure 5-8 illustrates t1 and t2 in memory. The code demonstrates the following three actions discussed so
far in the use of a class:
• Declaring a class
• Creating instances of the class
• Accessing the class members (that is, writing to a field and reading from a field)

class DaysTemp // Declare the class


{
public int High, Low; // Declare the instance fields
public int Average() // Declare the instance method
{
return (High + Low) / 2;
}
}

class Program
{
static void Main()
{
// Create two instances of DaysTemp
DaysTemp t1 = new DaysTemp();
DaysTemp t2 = new DaysTemp();

// Write to the fields of each instance


t1.High = 76; t1.Low = 57;
t2.High = 75; t2.Low = 53;

// Read from the fields of each instance and call a method of


// each instance
Console.WriteLine("t1: {0}, {1}, {2}",
t1.High, t1.Low, t1.Average() );
Console.WriteLine("t2: {0}, {1}, {2}",
t2.High, t2.Low, t2.Average () );

} Field Field Method

75

Das könnte Ihnen auch gefallen