Data Types
An important part in the life of a C# programmer is the management of data types. Just as in C and C++, every variable has a fixed data type. Every data type provides a rich set of operators that can be used to perform a very special operation. Objects can be seen as data types as well, but we'll take a closer look at that later in this book.
Predefined Data Types
C# supports a list of predefined data types. In this section, we examine the most important types. Table 3.1 contains an overview.
Table 3.1 Predefined Data Types
C# |
Mono |
Signed |
Memory |
Range |
sbyte |
System.Sbyte |
Yes |
1 byte |
128 to 127 |
short |
System.Int16 |
Yes |
2 bytes |
32768 to 32767 |
int |
System.Int32 |
Yes |
4 bytes |
2147483648 to 2147483647 |
long |
System.Int64 |
Yes |
8 bytes |
9223372036854775808 to 9223372036854775807 |
byte |
System.Byte |
No |
1 byte |
0 to 255 |
ushort |
System.Uint16 |
No |
2 bytes |
0 to 65535 |
uint |
System.Uint32 |
No |
4 bytes |
0 to 4294967295 |
ulong |
System.Uint64 |
No |
8 bytes |
0 to 18446744073709551615 |
float |
System.Single |
Yes |
4 bytes |
1.5x10-45 to 3.4 x x1038 |
double |
System.Double |
Yes |
8 bytes |
5.0x10-324 to 1.7x10308 |
decimal |
System.Decimal |
Yes |
12 bytes |
1.0x10-28 to 7.9x1028 |
char |
System.Char |
|
2 bytes |
Unicode characters |
boolean |
System.Boolean |
|
1 byte |
True or false |
After this brief overview, we'll show you how variables and data types can be used efficiently. The following example shows how a variable can be declared and displayed on screen:
using System; class Hello { public static void Main() { int x = 3; Console.WriteLine("The value is " + x ); } }
Declaring a variable works just like in C and C++. The way data is displayed is reminiscent of Java. We use the plus operator to connect two strings with each other. The output is not surprising:
[hs@localhost csharp]$ mono hello4.exe The value is 3
Using the plus operator is truly easy, but it could also be a danger. In most cases, the plus operator is used as a mathematical operator. If it's used differently, the result might be a bit unexpected:
using System; class Hello { public static void Main() { uint x = 3; Console.WriteLine("The value is " + x + 1 ); Console.WriteLine("The value is " + (x + 1) ); } }
We perform two operations that look pretty similar. However, the results differ significantly:
[hs@localhost csharp]$ mono hello5.exe The value is 31 The value is 4
The first operation connects two strings. The second example performs an addition. As you can see, the plus operator has more than just one meaning.
One-Dimensional Arrays
It's frequently necessary to store more than just one value in a variable. Fields can be used for this purpose. Arrays are needed to store a set of information. The following example explains how things work:
using System; class Hello { public static int Main() { String[] son = { "Hans", "Peter", "Olaf" }; Console.WriteLine("Son: " + son[0] ); String[] daughter = new string[3] { "Paula", "Petra", "Clara" }; Console.WriteLine("Daughter: " + daughter[2] ); return 1; } }
Two arrays are created. Names of men are stored in the first array. To tell Mono that the array called son contains more than just one value, we have to use brackets. To read data in an array, indexes are required. Just as in most other programming languages, the index of an array does not start with 1 but with 0. In the first part of our example, we try to access the array.
The second part of the example shows a slightly different way to create an array. In this case, the size of the array is defined explicitly. Again the elements in the array are accessed using an index. Let's see what comes out when the program is executed:
[hs@duron csharp]$ mono array.exe Son: Hans Daughter: Clara
The values in an array usually aren't static. To modify a value inside an array, the = operator can be called. The next example documents how a value inside an array can be overwritten and how to modify an array:
using System; class Hello { public static int Main() { String[] daughter = new string[3] { "Paula", "Petra", "Clara" }; tochter[2] = "Eva-Maria"; Console.WriteLine("daughter: " + tochter[2] ); return 1; } }
In this scenario, the third element is replaced with a string that's longer than the original value. The main idea of this example is that you need not worry about memory management anymore because this task is done by Mono. C programmers will know that the operation we've performed is not that simple when using C or C++.
The result is displayed as expected:
[hs@duron csharp]$ mono array.exe daughter: Eva-Maria
Multidimensional Arrays
Multidimensional arrays enable the user to implement complex and efficient data structures. Whenever it's necessary to store combinations of different elements, multidimensional arrays are definitely a good idea. C# provides a rich set of sophisticated methods for managing data structures. The following example shows how names can be stored:
using System; class Hello { public static int Main() { string[ , ] data = new string[2, 2]; data[0, 0] = "Paul"; data[1, 0] = "Rudi"; data[0, 1] = "Ruth"; data[1, 1] = "Evi"; Console.WriteLine("Value: " + data[1, 1] ); return 1; } }
As you can see in the example, many names are stored in a two-dimensional array. Women can be found in the second axis. The first axis enables us to access the names. The next listing shows what the results look like:
[hs@duron csharp]$ mono marray.exe Value: Evi
When working with arrays, it can be interesting to find out how many axes an array contains. In most programming languages, multidimensional data structures are truly painful. However, in C#, those problems have nearly been solved for you because the programming language itself does most of the work. Let's have a look at an additional example:
using System; class Hello { public static int Main() { string[ , ] a = new string[2, 2]; string[ ,,, ] b = new string[2, 2, 2, 2]; Console.WriteLine("a: " + a.Rank ); Console.WriteLine("b: " + b.Rank ); return 1; } }
At the beginning of the program, two arrays are created. The number of axes can be found in a variable called Rank.
Let's see which result we get:
[hs@duron csharp]$ mono main.exe a: 2 b: 4