Text Size

VALUE DATA TYPES

PDF 

Tutorials

(0 votes, average: 0 out of 5)

INTRODUCTION

C# distinguishes between two (2) different data types categories: value and reference types. Value data types directly store their value while reference data types store a reference to their value. Value types are stored on the stack whereas reference types are stored on the managed heap.

USING VALUE DATA TYPES

C# has some built-in value types such as integer, floating point numbers and Boolean values:

1. Integer

a. sbyte

b. short

c. int

d. long

e. byte

f. ushort

g. uint

h. ulong

2. Floating Point Types

a. float

b. double

3. Decimal Type

a. decimal

4. Boolean Type

a. bool

5. Character Type

a. char

Integer data types starting with “u” are unsigned data types. They take only positive values. An integer of type int occupies 32 bits and takes values between (-231,231-1), while an integer of type uint occupies 32 bits but takes values between (0,232-1). A short integer type is a 16-bit signed integer, while a long is defined as a 64-bit signed integer. Notice that byte is the only integer type not starting with a “u” and it is the unsigned representative of sbyte. As far as the floating types are concerned, a float is a 32-bit single precision floating point and a double is a 64-bit double precision floating point. The float type is used when less precision is required. Even more accuracy offers the decimal data type that occupies 128 bits and is used for financial calculation. This data type offers customizable accuracy; you can track smaller amounts with greater accuracy and larger amounts more rounded. Finally, bool types contain either true or false values and char represents a single 16-bit Unicode character. It is not possible to convert between 0,1 values to False,True. Literals of type char are signified by being enclosed in single quotes, for example ‘A’. Should you try to enclose a character in double quotes the compiler will produce an error. Characters can also represent escape sequences:

· Single quote : \’

· Double quote: \”

· Backslash: \\

· Null: \0

· Alert: \a

· Backspace: \b

· Form feed: \f

· Newline: \n

· Carriage return: \r

· Tab character: \t

We should note here that the compiler optimizes the use of 32-bit integer types ( signed or unsigned) and they should be preferred for counters and other frequently used variables.

The .Net framework contains many more value types than defined here but the types shown here are the most useful and cover most of your needs. Also, despite the fact that these types are value types, they still inherit for System.Object. This means you can call methods on them such as .ToString() and others.

Value types have an implicit constructor and thus, they are instantiated at the moment of declaration. The constructor assigns the value null or 0 in uninitialized data types variables. However you should always initialize the variable within the declaration:

bool myBool = false;

Trackback(0)
Comments (0)add comment

Write comment

busy

Site Statistics

Stats
Total Members
: 542
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1