
8 Language Overview
8.2 Types
8.2.1 Predefined typesC# provides a set of predefined types, most of which will be familiar to C and C++ developers. The predefined reference types are object and string. The type object is the ultimate base type of all other types. The type string is used to represent Unicode string values. Values of type string are immutable. The predefined value types include signed and unsigned integral types, floating-point types, and the types bool, char, and decimal. The signed integral types are sbyte, short, int, and long; the unsigned integral types are byte, ushort, uint, and ulong; and the floating-point types are float and double. The bool type is used to represent boolean values: values that are either true or false. The inclusion of bool makes it easier to write self-documenting code, and also helps eliminate the all-too-common C++ coding error in which a developer mistakenly uses "=" when "==" should have been used. In C#, the example int i = ...; F(i); if (i = 0) // Bug: the test should be (i == 0) G();results in a compile-time error because the expression i = 0 is of type int, and if statements require an expression of type bool. The char type is used to represent Unicode characters. A variable of type char represents a single 16-bit Unicode character. The decimal type is appropriate for calculations in which rounding errors caused by floating point representations are unacceptable. Common examples include financial calculations such as tax computations and currency conversions. The decimal type provides 28 significant digits. The table below lists the predefined types, and shows how to write literal values for each of them.
| Type | Description | Example |
|---|---|---|
| object | The ultimate base type of all other types | object o = null; |
| string | String type; a string is a sequence of Unicode characters | string s = "hello"; |
| sbyte | 8-bit signed integral type | sbyte val = 12; |
| short | 16-bit signed integral type | short val = 12; |
| int | 32-bit signed integral type | int val = 12; |
| long | 64-bit signed integral type | long val1 = 12;long val2 = 34L; |
| byte | 8-bit unsigned integral type | byte val1 = 12; |
| ushort | 16-bit unsigned integral type | ushort val1 = 12; |
| uint | 32-bit unsigned integral type | uint val1 = 12;uint val2 = 34U; |
| ulong | 64-bit unsigned integral type | ulong val1 = 12;ulong val2 = 34U;ulong val3 = 56L;ulong val4 = 78UL; |
| float | Single-precision floating point type | float val = 1.23F; |
| double | Double-precision floating point type | double val1 = 1.23;double val2 = 4.56D; |
| bool | Boolean type; a bool value is either true or false | bool val1 = true;bool val2 = false; |
| char | Character type; a char value is a Unicode character | char val = 'h'; |
| decimal | Precise decimal type with 28 significant digits | decimal val = 1.23M; |
using System;
class Test
{
static void Main() {
string s = "Test";
string t = string.Copy(s);
Console.WriteLine(s == t);
Console.WriteLine((object)s == (object)t);
}
}
produces the output True Falsebecause the first comparison compares two expressions of type string, and the second comparison compares two expressions of type object.
| |
| Jagger Software Ltd | |
| Company # 4070126 | |
| VAT # 762 5213 42 |