Professional C# 6 and .NET Core 1.0. Christian Nagel
Чтение книги онлайн.
Читать онлайн книгу Professional C# 6 and .NET Core 1.0 - Christian Nagel страница 21
![Professional C# 6 and .NET Core 1.0 - Christian Nagel Professional C# 6 and .NET Core 1.0 - Christian Nagel](/cover_pre243311.jpg)
Predefined Value Types
The built-in .NET value types represent primitives, such as integer and floating-point numbers, character, and Boolean types.
Integer Types
C# supports eight predefined integer types, shown in the following table.
Some C# types have the same names as C++ and Java types but have different definitions. For example, in C# an int is always a 32-bit signed integer. In C++ an int is a signed integer, but the number of bits is platform-dependent (32 bits on Windows). In C#, all data types have been defined in a platform-independent manner to allow for the possible future porting of C# and .NET to other platforms.
A byte is the standard 8-bit type for values in the range 0 to 255 inclusive. Be aware that, in keeping with its emphasis on type safety, C# regards the byte type and the char type as completely distinct types, and any programmatic conversions between the two must be explicitly requested. Also be aware that unlike the other types in the integer family, a byte type is by default unsigned. Its signed version bears the special name sbyte.
With .NET, a short is no longer quite so short; it is now 16 bits long. The int type is 32 bits long. The long type reserves 64 bits for values. All integer-type variables can be assigned values in decimal or hex notation. The latter requires the 0x prefix:
If there is any ambiguity about whether an integer is int, uint, long, or ulong, it defaults to an int. To specify which of the other integer types the value should take, you can append one of the following characters to the number:
You can also use lowercase u and l, although the latter could be confused with the integer 1 (one).
Floating-Point Types
Although C# provides a plethora of integer data types, it supports floating-point types as well.
The float data type is for smaller floating-point values, for which less precision is required. The double data type is bulkier than the float data type but offers twice the precision (15 digits).
If you hard-code a non-integer number (such as 12.3), the compiler will normally assume that you want the number interpreted as a double. To specify that the value is a float, append the character F (or f) to it:
The Decimal Type
The decimal type represents higher-precision floating-point numbers, as shown in the following table.
One of the great things about the .NET and C# data types is the provision of a dedicated decimal type for financial calculations. How you use the 28 digits that the decimal type provides is up to you. In other words, you can track smaller dollar amounts with greater accuracy for cents or larger dollar amounts with more rounding in the fractional portion. Bear in mind, however, that decimal is not implemented under the hood as a primitive type, so using decimal has a performance effect on your calculations.
To specify that your number is a decimal type rather than a double, a float, or an integer, you can append the M (or m) character to the value, as shown here:
The Boolean Type
The C# bool type is used to contain Boolean values of either true or false.
You cannot implicitly convert bool values to and from integer values. If a variable (or a function return type) is declared as a bool, you can only use values of true and false. You get an error if you try to use zero for false and a nonzero value for true.
The Character Type
For storing the value of a single character, C# supports the char data type.
Literals of type char are signified by being enclosed in single quotation marks – for example, 'A'. If you try to enclose a character in double quotation marks, the compiler treats the character as a string and throws an error.
As well as representing chars as character literals, you can represent them with four-digit hex Unicode values (for example, '\u0041'), as integer values with a cast (for example, (char)65), or as hexadecimal values (for example,'\x0041'). You can also represent them with an escape sequence, as shown in the following table.
Predefined Reference Types
C# supports two predefined reference types, object and string, described in the following table.
The object Type
Many programming languages and class hierarchies provide a root type, from which all other objects in the hierarchy are derived. C# and .NET are no exception. In C#, the object type is the ultimate parent type from which all other intrinsic and user-defined types are derived. This means that you can use the object type for two purposes:
• You can use an object reference to bind to an object of any particular subtype. For example, in Chapter 8, “Operators and Casts,” you see how you can use the object type to box a value object on the stack to move it to the heap; object references are also useful in reflection, when code must manipulate objects whose specific types are unknown.
• The object type implements a number of basic, general-purpose methods, which include Equals, GetHashCode, GetType, and ToString. Responsible user-defined classes might need to provide replacement implementations of some of these methods using an object-oriented technique known as overriding, which is discussed in Chapter 4, “Inheritance.” When you override ToString, for example, you equip your class with a method for intelligently providing a string representation of itself. If you don’t provide your own implementations for these methods in your classes, the compiler picks up the implementations in object, which might or might not be correct or sensible in the context of your classes.
You examine the object type in more detail in subsequent chapters.
The string Type
C# recognizes the string keyword, which under the hood is translated to the .NET class, System.String. With it, operations like string concatenation and string copying are a snap:
Despite this style of assignment, string is a reference type. Behind the scenes, a string object is allocated on the heap, not the stack; and when you assign one string variable to another string, you get two references to the same string in memory. However, string differs from the usual behavior for reference types. For example, strings are immutable. Making changes to one of these strings creates an entirely