< Day Day Up > |
The next three sections of this chapter describe features that you'll find in most programming languages: variables and data types, operators, expressions, and statements that control the flow of operations. The discussion begins with primitives. As the name implies, these are the core C# data types used as building blocks for more complex class and structure types. Variables of this type contain a single value and always have the same predefined size. Table 2-3 provides a formal list of primitives, their corresponding core data types, and their sizes.
As the table shows, primitives map directly to types in the base class library and can be used interchangeably. Consider these statements: System.Int32 age = new System.Int32(17); int age = 17; System.Int32 age = 17; They all generate exactly the same Intermediate Language (IL) code. The shorter version relies on C# providing the keyword int as an alias for the System.Int32 type. C# performs aliasing for all primitives. Here are a few points to keep in mind when working with primitives:
The remainder of this section offers an overview of the most useful primitives with the exception of string, which is discussed later in the chapter. decimalThe decimal type is a 128-bit high-precision floating-point number. It provides 28 decimal digits of precision and is used in financial calculations where rounding cannot be tolerated. This example illustrates three of the many methods available to decimal type. Also observe that when assigning a literal value to a decimal type, the M suffix must be used. decimal iRate = 3.9834M; // decimal requires M iRate = decimal.Round(iRate,2); // Returns 3.98 decimal dividend = 512.0M; decimal divisor = 51.0M; decimal p = decimal.Parse("100.05"); // Next statement returns remainder = 2 decimal rem = decimal.Remainder(dividend,divisor); boolThe only possible values of a bool type are true and false. It is not possible to cast a bool value to an integer for example, convert true to a 1, or to cast a 1 or 0 to a bool. bool bt = true; string bStr = bt.ToString(); // returns "true" bt = (bool) 1; // fails charThe char type represents a 16-bit Unicode character and is implemented as an unsigned integer. A char type accepts a variety of assignments: a character value placed between individual quote marks (' '); a casted numeric value; or an escape sequence. As the example illustrates, char also has a number of useful methods provided by the System.Char structure: myChar = 'B'; // 'B' has an ASCII value of 66 myChar = (char) 66; // Equivalent to 'B' myChar = '\u0042'; // Unicode escape sequence myChar = '\x0042'; // Hex escape sequence myChar = '\t'; // Simple esc sequence:horizontal tab bool bt; string pattern = "123abcd?"; myChar = pattern[0]; // '1' bt = char.IsLetter(pattern,3); // true ('a') bt = char.IsNumber(pattern,3); // false bt = char.IsLower(pattern,0); // false ('1') bt = char.IsPunctuation(pattern,7); // true ('?') bt = char.IsLetterOrDigit(pattern,1); // true bt = char.IsNumber(pattern,2); // true ('3') string kstr="K"; char k = char.Parse(kstr); byte, sbyteA byte is an 8-bit unsigned integer with a value from 0 to 255. An sbyte is an 8-bit signed integer with a value from 128 to 127. byte[] b = {0x00, 0x12, 0x34, 0x56, 0xAA, 0x55, 0xFF}; string s = b[4].ToString(); // returns 170 char myChar = (char) b[3]; short, int, longThese represent 16-, 32-, and 64-bit signed integer values, respectively. The unsigned versions are also available (ushort, uint, ulong). short i16 = 200; i16 = 0xC8 ; // hex value for 200 int i32 = i16; // no casting required single, doubleThese are represented in 32-bit single-precision and 64-bit double-precision formats. In .NET 1.x, single is referred to as float.
float xFloat = 24567.66F; int xInt = Convert.ToInt32(xFloat); // returns 24567 int xInt2 = (int) xFloat; if(xInt == xInt2) { } // False string xStr = Convert.ToString(xFloat); single zero = 0; if (Single.IsNaN(0 / zero)) { } // True double xDouble = 124.56D; Note that the F suffix is used when assigning a literal value to a single type, and D is optional for a double type. Using Parse and TryParse to Convert a Numeric StringThe primitive numeric types include Parse and tryParse methods that are used to convert a string of numbers to the specified numeric type. This code illustrates: short shParse = Int16.Parse("100"); int iParse = Int32.Parse("100"); long lparse = Int64.Parse("100"); decimal dParse = decimal.Parse("99.99"); float sParse = float.Parse("99.99"); double dbParse = double.Parse("99.99"); TRyParse, introduced in .NET 2.0, provides conditional parsing. It returns a boolean value indicating whether the parse is successful, which provides a way to avoid formal exception handling code. The following example uses an Int32 type to demonstrate the two forms of tryParse: int result; // parse string and place result in result parameter bool ok = Int32.TryParse("100", out result); bool ok = Int32.TryParse("100", NumberStyles.Integer, null, out result); In the second form of this method, the first parameter is the text string being parsed, and the second parameter is a NumberStyles enumeration that describes what the input string may contain. The value is returned in the fourth parameter. |
< Day Day Up > |