Understanding Data Types

   

In any programming language, it's critical that the compiler, the part of the Visual Studio framework that interprets the code you write into a language the computer can understand, fully understands 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 / "Dog" 
graphics/newterm.gif

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 will halt execution and display an exception (error) when it reaches the confusing line of code. (These two types of errors are discussed in detail in Hour 16, "Debugging Your Code.") Obviously, you can't subtract 659 by the word "Dog"; these two values are different types of data. In C#, these two values are said to have two different data types. In C#, constants, variables , and arrays must always be defined to hold a specific type of information.

Determining Data Type

graphics/newterm.gif

Data typing ”the act of defining a constant, a variable, or an array's data type ”can be confusing. To C#, a number is not a number. A number that contains a decimal value is different from a number that does not. 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. Because of this limitation, you must give careful consideration to the type of data you plan to store in a constant, a variable, or an array at the time you define it. 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. Table 12.1 lists the C# data types and the range of values they can contain.

Table 12.1. The C# Data Types
Data Type ”Value Value Range
bool true or false
byte 0 to 255
char a single character
decimal “79,228,162,514,264,337,593,543,950,335 to “7.9228162514264337593543950335. Use this data type for currency values
double “1.79769313486232E308 to “4.94065645841247E-324 for negative values; 4.94065645841247E “324 to 1.79769313486232E308 for positive values
float “3.402823E38 to “1.401298E “45 for negative values; 1.401298E “45 to 3.402823E38 for positive values
int “2,147,483,648 to 2,147,483,647.
long “9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
sbyte “128 to 127
short “32,768 to 32,767
uint Integers in the range from 0 to 4,294,967,295
ulong Integers in the range from 0 to 10^20
ushort Integers in the range from 0 to 65,535
Data Type ”Reference Value Range
string 0 to approximately 2 billion characters
object Any type can be stored in a variable type Object

C# supports unsigned data types for short, int, and long (the types prefaces 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).

Tips for Determining Data Type

The list of data types may 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.

  • If you want to store only the values 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.

  • If you need to store numbers with no decimal places but with values larger or smaller than short allows, use the int or 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.

  • If you need to store currency amounts, use the decimal data type.

  • If you need to store a single character, use the char data type.

Casting Data from One Data Type to Another

graphics/newterm.gif

Under some circumstances, 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. 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 higher value than does a float.

Explicit casting is required when a potential exists for data loss or when converting a larger data type into a smaller data type. 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, C# requires that these types of conversions be explicitly written using the cast operator. For instance, you can set the value of a variable declared as short to the value of a variable declared as integer using the following syntax:

 short MyShortInterger; int MyInteger = 1000; MyShortInterger = (short) MyInteger; 

Notice here that 1000 would fit in a short, so data wouldn't actually be lost if no explicit cast were performed. However, C# doesn't care; it's the potential for data loss that causes C# to require explicit casts.

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

Table 12.2. Safe Conversions
Type Can Be Safely Converted To
byte char, short, int, long, float, double, decimal
short int, long, float, double, decimal
int long, float, double, decimal
long float, double, decimal
float double, decimal
double decimal

   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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