Data Types

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:

  • Value types

  • Reference types

  • Pointer types

We'll take a look at each of these categories in turn .

Value Types

When you copy value types, a copy is made of their values (hence the name ). Value types are broken into two categories:

  • Enumeration types

  • Struct types

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:

  • Integral

  • Floating-point

  • Decimal

  • Boolean

These simple types are often what people mean when they talk about data storage.

Integral Types

Integral 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

TYPE

RANGE

SIZE

sbyte

128 to 127

Signed 8-bit integer

byte

0 to 255

Unsigned 8-bit integer

char

U+0000 to U+ffff

Unicode 16-bit character

short

32,768 to 32,767

Signed 16-bit integer

ushort

0 to 65,535

Unsigned 16-bit integer

int

2,147,483,648 to 2,147,483,647

Signed 32-bit integer

uint

0 to 4,294,967,295

Unsigned 32-bit integer

long

9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Signed 64-bit integer

ulong

0 to 18,446,744,073,709,551,615

Unsigned 64-bit integer

Floating-Point Types

Floating-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.

FOR C++ PROGRAMMERS

In C#, the long data type is 64 bits, whereas in C++, it is often 32 bits.


Table 1.3. The C# Floating-Point Types

TYPE

APPROXIMATE RANGE

PRECISION

float

±1.5 ? 10 -45 to ±3.4 ? 10 38

7 digits

double

±5.0 ? 10 -324 to ±1.7 ? 10 308

15-16 digits

Decimal Types

The 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

TYPE

APPROXIMATE RANGE

PRECISION

decimal

±1.0x1028 to ±7.9x1028

2829 significant digits

Boolean Types

Boolean values are used to declare variables to store the Boolean values, true and false .

FOR C++ PROGRAMMERS

Unlike in C++, data conversions between the bool type and other types (in particular, int ) are not allowed in C#.


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

C# TYPE

.NET FRAMEWORK TYPE

bool

System.Boolean

byte

System.Byte

sbyte

System.SByte

char

System.Char

decimal

System.Decimal

double

System.Double

float

System.Single

int

System.Int32

uint

System.UInt32

long

System.Int64

ulong

System.UInt64

object

System.Object

short

System.Int16

ushort

System.UInt16

string

System.String

Reference Types

Reference 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:

  • class

  • interface

  • delegate

There are also some built-in reference types in C#:

  • object

  • string

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.

Pointers

Pointers 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.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net