Simple types include types such as numbers and Boolean (true or false) values that make up the fundamental building blocks for your applications. Unlike complex types, simple types cannot have children or attributes. Most of the simple types available are numeric, which at first glance seems a bit

strange — surely, you only need one type to store a number?

The reason for the plethora of numeric types is because of the mechanics of storing numbers as a series of 0s and 1s in the memory of a computer. For integer values, you simply take a number of bits (individual digits that can be 0 or 1) and represent your number in binary format. A variable storing

N bits allows you to represent any number between 0 and (2 N – 1). Any numbers above this value are too big to fit into this variable.

For example, suppose you have a variable that can store 2 bits. The mapping between integers and the bits representing those integers is therefore as follows:

0 = 00

1 = 01

2 = 10

3 = 11

The inevitable result of this system is that you would need an infinite number of bits to be able to store every imaginable number, which isn ’ t going to fit in your trusty PC. Even if there were a quantity of bits you could use for every number, it surely wouldn ’ t be efficient to use all these bits for a variable that, for example, was required to store only the numbers between 0 and 10 (because storage would be wasted). Four bits would do the job fine here, enabling you to store many more values in this range in the same space of memory.

Instead, a number of different integer types can be used to store various ranges of numbers, which take up differing amounts of memory (up to 64 bits). These types are shown in the following table.

Type | Alias For | Allowed Values |
---|---|---|

sbyte | System.SByte | Integer between – 128 and 127 |

byte | System.Byte | Integer between 0 and 255 |

short | System.Int16 | Integer between – 32768 and 32767 |

ushort | System.UInt16 | Integer between 0 and 65535 |

int | System.Int32 | Integer between – 2147483648 and 2147483647 |

uint | System.UInt32 | Integer between 0 and 4294967295 |

long | System.Int64 | Integer between – 9223372036854775808 and 9223372036854775807 |

ulong | System.UInt64 | Integer between 0 and 18446744073709551615 |

The u characters before some variable names are shorthand for unsigned , meaning that you can ’ t store negative numbers in variables of those types, as shown in the Allowed Values column of the table.

Of course, you also need to store floating – point values, those that aren ’ t whole numbers. You can use three floating – point variable types: float , double , and decimal . The first two store floating points in the form +/ – m × 2e , where the allowed values for m and e differ for each type. decimal uses the alternative form +/ – m × 10e . These three types are shown in the following table, along with their allowed values of m and e , and these limits in real numeric terms:

Type | Alias For | Min M | Max M | Min E | Max E | Approx.Min Value | Approx.Max Value |
---|---|---|---|---|---|---|---|

float | System.Single | 0 | 224 | -149 | 104 | 1.5×10^{-45} |
3.4×10^{38} |

double | System.Double | 0 | 253 | -1075 | 970 | 5.0×10^{-324} |
1.7×10^{308} |

decimal | System.Decimal | 0 | 296 | -26 | 0 | 1.0×10^{-28} |
7.9×10^{28} |

In addition to numeric types, three other simple types are available:

Type | Alias For | Allowed Values |
---|---|---|

char | System.char | Single Unicode character, stored as an integer between 0 and 65535 |

bool | System.Boolean | Boolean value, true or false |

string | System.String | A sequence of characters |

Note that there is no upper limit on the amount of characters making up a string , because it can use varying amounts of memory.

The Boolean type bool is one of the most commonly used variable types in C#, and indeed similar types are equally prolific in code in other languages. Having a variable that can be either true or false has important ramifications when it comes to the flow of logic in an application. As a simple example, consider how many questions can be answered with true or false (or yes and no). Performing comparisons between variable values or validating input are just two of the programmatic uses of Boolean variables that you will examine very soon.