Understanding Data Types


In every programming language there is a compiler. The compiler is the part of the Visual Studio .NET Framework that interprets the code you write into a language the computer can understand. The compiler must understand the type of data you're manipulating in code. For example, if you asked the compiler to add the following values, it would get confused:

659 + "Fender"


When the compiler gets confused, it either refuses to compile the code (which is the preferred situation because you can address the problem before your users run the application), or it halts execution and displays an exception (error) when it reaches the confusing line of code. (These two types of errors are discussed in detail in Hour 15, "Debugging Your Code.") Obviously, you can't add the word Fender to the number 659 because these two values are different types of data. In Visual C#, these two values are said to have two different data types. In Visual C#, constants, variables, and arrays must always be defined to hold a specific type of information.

Determining Data Type

Data typingthe act of defining a constant, variable, or array's data typecan be confusing. To Visual C#, a number is not simply a number. A number that contains a decimal value is different from a number that doesn't. Visual C# can perform arithmetic on numbers of different data types, but you can't store data of one type in a variable with an incompatible type. Visual C# supports two categories of data types: value types and reference types. The main difference between these two types is how their values are stored in memory. As you continue to create more complex applications, this difference may have an impact on your programming. For this book, however, this distinction is minimal. Because of this limitation, you must give careful consideration to the type of data you plan to store in a constant, variable, or array at the time you define it. Table 11.1 lists the Visual C# data types and the range of values each one can contain.

Table 11.1. The Visual C# Data Types

Data TypeValue

Value Range

bool

True or False

byte

0 to 255 (unsigned)

char

A Unicode character

DateTime

0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM on December 31, 9999

decimal

+/7.9228162514264337593543950335 with 28 places to the right of the decimal; use this data type for currency values

double

1.79769313486231570E+308 through 1.79769313486231570E+308

int

2, 147,483,648 to 2,147,483,647 (signed). This is the same as the data type Int32.

long

9, 223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed). This is the same as data type Int64.

sbyte

128 through 127 (signed)

short

32,768 to 32,767 (signed). This is the same as data type Int16.

float

3.4028235e38 through 3.4028235e+8

uint

0 through 4,294,967,295 (unsigned).

ulong

0 through 18,446,744,073,709,551,615 (1.8...E+19) (unsigned)

ushort

0 through 65,535 (unsigned)

Data TypeReference

Value Range

string

0 to approximately 2 billion Unicode characters

Object

Any type can be stored in a variable


Visual C# supports unsigned data types for short, int, and long (the types prefaced with u, such as uint). Because negative numbers are excluded (there is no sign), this has the effect of doubling the positive values for a short, an int, or a long. Signed data types are preferable and should be used unless you have a very good reason for doing otherwise (such as declaring a variable that will never hold a negative value).

Did you Know?

Tips for Determining Data Type

The list of data types might seem daunting at first, but you can follow some general guidelines for choosing among them. As you become more familiar with the different types, you'll be able to fine-tune your data type selection.

Following are some helpful guidelines for using data types:

  • If you want to store text, use the string data type. The string data type can be used to store any valid keyboard character, including numbers and nonalphabetic characters.

  • To store only the value True or False, use the bool data type.

  • If you want to store a number that contains no decimal places and is greater than 32,768 and smaller than 32,767, use the short data type.

  • To store numbers with no decimal places, but with values larger or smaller than short allows, use the int or long (an abbreviation for integer and long) data types.

  • If you need to store numbers that contain decimal places, use the float data type. The float data type should work for almost all your values containing decimals, unless you're writing incredibly complex mathematical applications or need to store very large numbers; in that case, use a double.

  • To store currency amounts, use the decimal data type.

  • To store a single character, use the char data type.

  • If you need to store a date and/or a time value, use the DateTime data type. When you use the DateTime data type, Visual C# recognizes common date and time formats. For example, if you store the value 7/22/2001, Visual C# doesn't treat it as a simple text string; it knows that the text represents July 22, 2001.

  • Different data types use different amounts of memory. To preserve system resources, it's best to use the data type that consumes the least amount of memory and still provides the ability to store the full range of possible values. For example, if only storing numbers from 1 to 10, use a short instead of a long.


By the Way

The Object data type requires special attention. If you define a variable or array as an Object data type, you can store just about any value you care to in it; Visual C# determines what data type to use when you set the variable's value.

Several drawbacks exist to using Object data types. First, Object data types take up more memory than the other data types. In addition, Visual C# takes a little longer to perform calculations on Object data types. Unless you have a specific reason to do soand there are valid reasons, such as when you don't know the type of data to be stored ahead of timedon't use the Object data type. Instead, become familiar with the explicit data types and use them appropriately.


Casting Data from One Data Type to Another

Under most circumstances, Visual C# won't allow you to move data of one type into a variable of another type. The process of changing a value's data type is known as casting. Visual C# supports two types of casting: implicit and explicit. Implicit conversions are done automatically by the compiler. These conversions guarantee that no data is lost in the conversion. For instance, you can set the value of a variable declared as double to the value of a variable declared as float without an explicit cast because there is no risk of losing data; the double data type holds a more precise value than does a float, and this type of cast is called a widening cast.

Explicit casting is required when a potential exists for data loss or when converting a larger data type into a smaller data type (a narrowing cast). If you tried to place a value in a variable when the value was higher than the variable's supported data type, some data would be lost. Therefore, Visual C# requires that these types of conversions be explicitly written using the cast operator.

Table 11.2 lists some of the type conversions that can be done implicitly with no loss of information.

Table 11.2. Safe Conversions

Type

Can Be Safely Converted To

byte

short, int, long, float, double, or decimal

short

int, long, float, double, or decimal

int

long, float, double, or decimal

long

float, double, or decimal

float

double

double

decimal


To explicitly convert data from one type to another, you use one of the methods of the Convert class. Table 11.3 lists these methods. The use of these methods is pretty straightforward: Pass the data to be cast as the parameter, and the method returns the value with the return type. For example, to place the value of a variable declared as double into a variable declared as single, you could use a statement such as the following:

sngVariable = Convert.ToSingle(dblVariable);


Table 11.3. Some Common Type Conversion Methods of the Convert Class

Function

Converts To

ToBoolean(expression)

bool

ToByte(expression)

byte

ToChar(expression)

char

ToDateTime(expression)

DateTime

ToDecimal(expression)

decimal

ToDouble(expression)

double

ToInt16(expression)

16-bit signed integer

ToInt32(expression)

integer

ToInt64(expression)

long

ToSByte(expression)

sbyte

ToSingle(expression)

Single

ToString(expression)

string

ToUInt16(expression)

16-bit unsigned integer

ToUInt32(expression)

32-bit unsigned integer

ToUInt64(expression)

64-bit unsigned integer





Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

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