1.6 Working with Numeric Types

 <  Day Day Up  >  

You need to store data in a variable as an integral type.


Technique

Choosing the correct data type can almost be considered an art. Keep in mind the range of possible values for the data you need to store in memory and whether you need a signed or unsigned data type.

Comments

There are eight types designed to work with numerical data. Each of the types is designated based on its size and whether it is signed. The smallest integral types available to C# are the 8-bit byte with a range of 0 to 255 and the 8-bit sbyte , a signed data type with a range of “128 to 127. At the far end of the spectrum are the 64-bit ulong and the signed version, the long . Table 1.1 shows the possible numerical data types as well as their sizes in bits and range.

Table 1.1. Integral Data Types and Their Associated Sizes and Ranges

Data Type

Size (Bits)

Range

sbyte

8

-128 to 127

byte

8

0 to 255

short

16

-32,768 to 32,767

ushort

16

0 to 65,535

char

16

0 to 65,535

int

32

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

uint

32

0 to 4,294,967,295

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

When working with integral data types, you have to weigh two options. First of all, even though we live in an age where memory is in abundance , you should still attempt to keep memory usage to a minimum. Sometimes, your application might need to run in a low-memory environment, and by choosing a design that optimizes memory usage, you can benefit from this strategy. Secondly, you must ensure that the data type you choose is large enough to hold any value you assign to it. If you inadvertently assign a number outside of a data type's range, your application will encounter a situation that it is not prepared to deal with. Based on your project properties and whether you are using exception handling, which is explained later in this book, the CLR will do one of two things. First, your application will throw an OverflowException . It is then your responsibility to handle this exception or face the consequences of an application crash.

By default, your project is created with overflow checking turned off. This default means that the CLR does not check whether a value being assigned to a data type causes an overflow. Rather, it takes a safer approach, which is either to flip the bits of the data type, thereby causing it to change sign, or to simply roll the value back to 0 in the case of unsigned data. As you might guess, this process can have undesirable side effects in your application and can lead to subtle defects that might be hard to find.

To turn on overflow checking in your project, open the property pages of your project by choosing Project, Properties (Alt+P). Select Configuration Properties, Build. Set the property labeled Check for Arithmetic Overflow to True so that data types are checked for overflow, as shown in Figure 1.1. If you believe that once you thoroughly test your code with all possible inputs your data types will not overflow, you can consider setting this property to False . The safest and most advantageous route is to enable this property when you build in debug mode and turn it off, as an optimization technique, during release builds.

Figure 1.1. Changing project properties to check for arithmetic overflows.

graphics/01fig01.gif

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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