Data handling is one of the most important tasks you'll perform in C#. Some data handling is rudimentary, as when you place a quoted string of text in your code, which is called a literal : Console.WriteLine("Hello from C#."); Numerical values can also be literals, as when we used 32 and 212 like this: Console.WriteLine("The temperature is between {0} and {1}", 32, 212); When it comes to manipulating your data under programmatic control, you have to go further than that. In general, C# data types fall into the following categories:
We'll take a look at each of these categories in turn . Value TypesWhen you copy value types, a copy is made of their values (hence the name ). Value types are broken into two categories:
Enumeration types are created with the enum keyword, as we'll see later in this chapter. The struct types contain the user -defined types you create with the struct keyword that we'll see in Chapter 3, "C# Object-Oriented Programming," as well as the following built-in simple types:
These simple types are often what people mean when they talk about data storage. Integral TypesIntegral types hold numbers such as -1 or 15. These types include integers (referred to as int types) and longs (called long types, which have twice the storage capacity of int types). You can see the data capacities of the various integral types in Table 1.2. Table 1.2. The C# Integral Types
Floating-Point TypesFloating-point types hold data like 3.14159 and -1.503. There are only two built-in floating-point data types in C#, float and double , as you can see in Table 1.3.
Table 1.3. The C# Floating-Point Types
Decimal TypesThe decimal type is a 128-bit data type. Compared to floating-point types, the decimal type has greater precision and a smaller range, which makes it suitable for financial and monetary calculations. You can see the range for this type in Table 1.4. Table 1.4. The Decimal Type
Boolean TypesBoolean values are used to declare variables to store the Boolean values, true and false .
Here's an important pointall of the simple types are aliases of the .NET Framework types. For example, int is really just another name for the .NET Framework's System.Int32 . For a complete list of aliases, see Table 1.5. Table 1.5. The .NET Types Corresponding to the C# Simple Types
Reference TypesReference types are not as simple as value types. When you create an object of a reference type, that object is stored in general memory allocated for the program, the heap (value types are stored on the stack). When you create an object of a reference type, you use the new operator, which returns a reference to that object. When you pass an object of a reference type to a method, you actually pass that reference, not a copy of the object itself (as you do with value types). Here are the keywords you use to declare reference types in C#, and we'll see them all in this book:
There are also some built-in reference types in C#:
The string type is a reference type, but it's so popular that it shares many aspects of value types in C#for example, you don't have to use the new operator when declaring a new string. PointersPointers are the third data type in C#, and they're strongly discouraged, used only if there's no other way (as when you have to pass a pointer to a Windows API function). You use pointers to "point" to locations in memory, and although pointers are a big part of C++, they've been put on the back burner in C# for security reasons. Although C# can still support pointers, they can only be used in C#'s "unsafe" mode, as we'll see in Chapter 13. That covers the C# data types and their capabilitiesnow it's time to put them to work by creating variables. |