Basic C# Types
A type is the organization and format of information. For instance, an integer type is a 32-bit number. C# language elements such as classes and structs permit creation of custom types. Some languages are weakly typed, and there are times when the interpretation of types in these weakly typed languages will cause program errors.
C# is a strongly typed language. This essentially means that the compiler and runtime system does a good job of verifying the type consistency of expressions. All variables have a type. The type produced by an expression always either is defined by the C# language or is a user-defined type. C# provides a mechanism for converting one type to another.
While discussing each type, this section also shows how to declare a literal of each type. Literals are values that can't be changed. They can't be referenced, either. They occupy the memory space where they're used. While it's possible to copy the value of a literal into a variable and then change the variable, this does not change the value of the original literal.
Variable Declarations
Variables are programming elements that can change during program execution. They're used as storage to hold information at any stage of computation. As a program executes, certain variables will change to support the goals of an algorithm. Every variable has a type, and this section will show how to specify a variable's type. The syntax of a variable definition always conforms to the following pattern:
Type Identifier [Initializer];
In this example, Type is a placeholder, representing one of the types listed in this section or a user-defined type. Every variable must have a Type part in its declaration. Similarly, every variable declaration must have an identifier or name. Declarations may optionally include an initializer to set the value of a variable when it is created. The type of the value used to initialize a variable must be compatible with the type that the variable is declared as.
NOTE
C# variables may be declared and initialized in the same statement. Examples in this chapter show initializations to provide a clearer understanding of how the type can be used. It would be correct to assume that when I use the term "declaration," both a declaration and an initialization could be happening at the same time.
The Simple Types
The simple types consist of Boolean and numeric types. The numeric types are further subdivided into integral and floating-Point types.
The Boolean Type
There's only a single Boolean type named bool. A bool can have a value of either true or false. The values true and false are also the only literal values that you can use for a bool. Here's an example of a bool declaration:
bool isProfitable = true;
NOTE
The bool type will not accept integer values such as 0, 1, or -1. The keywords true and false are built into the C# language and are the only allowable values.
For C++ Programmers
There is no casting conversion between int and bool types. To accomplish a similar affect, a bool result can be obtained with the equality operator. That is, (x == 0) would evaluate to true when the integer type x is equal to 0.
The Integral Types
The integral types are further subdivided into eight types plus a character type: sbyte, byte, short, ushort, int, uint, long, ulong, and char. All of the integral types except char have signed and unsigned forms. All integral type literals can be expressed in hexadecimal notation by prefixing 0x to a series of hexadecimal numbers 0 through F. The exception is the char.
A char holds a single Unicode character. Some examples of char variable declarations include:
char middleInitial; \\ uninitialized char yesNo = 'Y'; char studentGrade = '\u005A'; \\ Unicode 'Z' char studentGrade = '\x0041'; \\ Unicode 'A'
As shown previously, Unicode escape character notation requires four hexadecimal digits, prefixed by \u or \U. The digits are left padded with zeros to make the digit part four characters wide. A char may also be specified in hexadecimal notation by prefixing \x to between 1 and 4 hexadecimal digits.
For C++ Programmers
Notice the difference between the size of the C# char (16 bits) and the normal C++ char (8 bits). The C# char is similar to the C++ wchar_t (16 bits).
There are also special escape sequences representing characters. They're used for alert, special formatting, and building strings to avoid ambiguity. The following list shows the valid C# escape sequences:
\' Single Quote \" Double Quote \\ Backslash \0 Null \a Bell \b Backspace \f Form Feed \n Newline (linefeed) \r Carriage Return \t Horizontal Tab \v Vertical Tab
A byte is an unsigned type that can hold 8 bits of data. Their range is from 0 to 255. An sbyte is a signed byte with a range of 128 to 127. This is how you declare byte variables:
byte age = 25; sbyte normalizedTolerance = -1;
The short type is signed and holds 16 bits. It can hold a range from 32,768 to 32,767. The unsigned short, ushort, holds a range of 0 to 65,535. Here are a couple examples:
ushort numberOfJellyBeans = 62873; short temperatureFarenheit = -36;
The integer type is signed and has a size of 32 bits. The signed type, int, has a range of 2,147,483,648 to 2,147,483,647. The uint is unsigned and has a range of 0 to 4,294,967,295. Unsigned integers may optionally have a u or U suffix. Examples follow:
uint nationalPopulation = 4139276850; // also 4139276850u or 4139276850U int tradeDeficit = -2058293762;
A long type is signed and holds 64 bits with a range of 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. A ulong is unsigned with a range of 0 to 18,446,744,073,709,551,615. Unsigned long literals may have suffixes with the combination of uppercase or lowercase characters UL. Their declarations can be expressed like this:
ulong lightYearsFromEarth = 72038289347236792; // also 72038289347236792ul // or 72038289347236792UL // or 72038289347236792uL // or 72038289347236792Lu // or 72038289347236792LU // or 72038289347236792lU long negativeVariance = -1636409717646593274; // also 1636409717646593274l // or 1636409717646593274L
Each of the types presented to this point have a unique size and range. Table 2.3 provides a summary and quick reference of the size and range of each integral type.
Table 3 The Integral Types
Type |
Size(in Bits) |
Range |
char |
16 |
0 to 65,535 |
sbyte |
8 |
128 to 127 |
byte |
8 |
0 to 255 |
short |
16 |
32,768 to 32,767 |
ushort |
16 |
0 to 65,535 |
int |
32 |
2,147,483,648 to 2,147,483,647 |
uint |
32 |
0 to 4294967295 |
long |
64 |
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ulong |
64 |
0 to 18,446,744,073,709,551,615 |
For C++ Programmers
There is no native equivalent in C++ for the byte. However, there are ways of producing the same effect with typedef'd signed and unsigned chars. Also, a C++ long is 32 bits, whereas a C# long is 64 bits.
For Java Programmers
There are no unsigned types in Java, but there are in C#.
The Floating-Point Types
C# provides two floating-point typesfloat and doubleand a new type called decimal. The floating-point types conform to IEEE 754 specifications.
Floating-point literals may optionally be specified with exponential notation. This allows specification of very large numbers with the least amount of space necessary to write them. The trade-off between exponential and normal notation is size versus precision. The general form of exponential syntax is
N.Ne±P
where N is some decimal digit, e can be uppercase or lowercase, and P is the number of decimal places. The ± indicates either a +, -, or neither, which is the same as +. This is standard scientific notation.
The float type can hold a range of around 1.5 x 1045 to 3.4 x 1038. It has a seven-digit precision. To designate a floating-point literal as a float, add an F or f suffix. A float literal can be written with or without exponential notation, as follows:
float profits = 36592.73; // also 36592.73F // or 36592.73f float atomicWeight = 1.54e-15; float warpSpeed = 3.21E3;
A double has a range of about 5.0 x 10324 to 1.7 x 10308, and a precision of 15 to 16 digits. double literals may have the suffix D or d. It, too, may have literals expressed with or without exponential notation:
double vectorMagnitude = 8.2e127; double accumulatedVolume = 7982365.83658341; // also 7982365.83658341D // or 7982365.83658341d
A new type not seen in any other language is the decimal type. The decimal type has 28 or 29 digits of precision and can range from 1.0 x 1028 to about 7.9 x 1028. decimal literals can be specified with an M or m suffix. The trade-off between decimal and double is precision versus range. The decimal is the best choice when precision is required, but choose a double for the greatest range. The decimal type is well suited for financial calculations, as shown in the following example:
decimal annualSales = 99873582948769876589348317.95;
TIP
Use the C# decimal type for greater precision in financial calculations.
Table 4 provides a quick lookup of the floating-point types.
Table 4 The Floating-Point Types
Type |
Size(bits) |
Precision |
Range |
float |
32 |
7 digits |
1.5 x 1045 to 3.4 x 1038 |
double |
64 |
1516 digits |
5.0 x 10324 to 1.7 x 10308 |
decimal |
128 |
2829 decimal places |
1.0 x 1028 to 7.9 x 1028 |
A final word on literal suffixes: There are common suffixes for each literal type. Suffixes ensure that the literal is the intended type. This is good for documentation. However, the primary benefit is ensuring that your expressions are evaluated correctly; that is, the compiler will interpret float and decimal literals without suffixes as a double when evaluating an expression. To avoid the associated errors, use an appropriate literal suffix.
Struct Types
A struct is a value type. All of the types presented thus far fall into the value type category. Value types are variables that directly hold their data. They are allocated on the stack, which makes them very efficient for storing and retrieving information. Structs are containers that can hold a collection of other items. They provide a method for programmers to create their own value types.
Reference Types
There are four reference types in C#: classes, interfaces, delegates, and arrays. Reference types are library or user-defined types that are allocated on the heap. Being allocated on the heap means that reference types use more system resources. They are managed by a built-in garbage collector, which also manages their lifetimes. Classes may contain many other C# language members. They also define unique types.
Interfaces are used to expose the public attributes and behavior of classes. They have no implementations themselves. Whenever a class specifies an interface, a programmer knows, by the definition of that interface, that the class supports certain attributes and behavior. This way, a number of different classes can implement the same interface and be used in the same basic manner but provide their own unique behavior.
Delegates provide a type-safe way to dynamically reference class methods. When the exact method to implement won't be known until runtime, a delegate can be used to accept a reference to a method. Then whatever method is assigned to the delegate at runtime can be executed by calling the delegate to which the method was assigned. This is type-safe because a method must conform to the type specified in the delegate declaration before it is assigned to a delegate.
Arrays provide a method of storing multiple items of a specific type. Their interface represents a linear collection of data that can be referenced in sequence. Their power extends to providing specialized methods for managing their data. A C# array is a useful method of storing many items of the same type of data in a linear form.
Enumeration Types
The enum is a list of constant values. Elements of an enum are expressed in words rather than numbers, which makes it convenient for understanding the meaning of the value being used. Here's an example:
enum Months { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
For Java Programmers
Java does not have an equivalent to an enum.
By default, the first element of an enum starts with 0, unless specified otherwise. Subsequent elements have a value of 1 greater than their predecessor, unless they also have a designated value. In the following example, Mon has the value 1, Tue is 2, Wed is 3, Thu is 4, and Fri is 5. Then after Sat is changed to 10, Sun becomes 11.
enum Weekday { Mon = 1, Tue, Wed, Thu, Fri, Sat = 10, Sun };
The type of enum elements may be sbyte, byte, short, ushort, int, uint, long, or ulong. Specify this with a colon and type specification after the name. Here's an example:
enum Month: byte { January, February, March, April, May, June, July, August, September, October, November, December };
For C++ Programmers
In C#, enums can be specified as sbyte, byte, short, ushort, int, uint, long, or ulong, but in C++ they are int.
String Type
The string type is a C# primary type. Its value is represented by a string of Unicode characters. There are two types of string literals. The first type may be any valid set of characters between two double quotes, including character escape sequences.
string thankYou = "Grazie!\a"; // Grazie! <ding> string hello = "Sa-waht dee\tkrahp!"; // Sa-waht dee<tab>krahp! string kewl = "Das ist\nzehr\ngut!"; // Das ist // zehr // gut!
For C++ Programmers
A C++ string was originally the same as a normal C string, a pointer to a null-terminated array of characters. With the introduction of Standard C++, a C++ string now refers to the Standard Template Library (STL) string type. A C# string is a built-in type, and its representation is transparent.
The second type is a verbatim string literal. It's made by prefixing a string with an @. The difference between verbatim string literals and normal string literals is that the character escape sequences are not processed, but are interpreted as is. Because the double-quote escape sequence won't work, include two quotes side by side to include a single quote in a string. Verbatim string literals may span multiple lines, if needed. The following examples show various forms of the verbatim string literals.
string whoSaid = @"He said, ""She said."""; // He said, "She said." string beerPlease = @"Een \'Duvel\', alstublieft!"; // Een \'Duvel\', alstublieft! string gettysburg = @"Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the principle that all people are considered equal...";
For C++ and Java Programmers
C# includes a special type of string literal called the verbatim string literal. It's useful for avoiding escape sequences in file paths and similar situations where decorations detract from the readability of the string literal.