Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

FIGURE 2-3 Explicit data type conversion

In addition to the data type conversion statements, C# provides you with boxing and unboxing data conversion techniques. You can use boxing to convert a value type to an object type.Boxing can be of the type implicit or explicit data conversion.

When you try to convert a value type to an object type, C# creates an instance of the object type. The value stored in the data type is then written to the instance of the object that is created.

Conversely, unboxing converts an object type to a value type. Unboxing is an explicit data type conversion technique. To convert an object type to a value type by using unboxing, you first need to create a value type by using boxing. The concept of boxing and unboxing is explained in detail in Chapter 4, “More about Components.”

As you have seen earlier, C# uses variables to store values. To store multiple variables as a single data structure, you can use an array.

Arrays

An array is a data structure that acts as a pointer to an address in memory. An array stores a number of variables and has an index attached to it.The index of an

C# BASICS

Chapter 2

 

25

 

 

 

 

 

array is used to access the elements of the array. The elements of an array are the variables that are stored in the array. An array can store only the elements of the same data type. For example, an integer array can store only the variables of the integer type. Unlike C++, an array in C# is an object and, therefore, has methods and properties associated with it.

To access the elements in an array, you use indices. An array can have a single index or multiple indices attached to it. The number of indices on each element of an array defines the rank of the array. For example, if an array has one index attached to its elements, the rank of the array is one. Such an array is called a single-dimensional array. Similarly, if an array has more than one index, it is called a multidimensional array. You will learn more about multidimensional arrays in Chapter 4, in the section “Multidimensional Arrays.”

To use an array in a program code, you need to declare and initialize it. In C#, you initialize an array by using the new keyword. To initialize an integer array with 20 elements, you use the following statement:

int [] Integer = new int [20];

C# allows you to specify the size of an array dynamically. Therefore, you can declare an array and initialize it at run time. When you declare an array without initializing it, C# creates a null reference to the array. You can then specify the amount of memory required by using the new keyword. C# allocates the required memory to the array at run time. For example, you can declare an array Integer and then specify the size of the array as 20 by using the new keyword.

int [] Integer;

Integer = new int [20];

When the array is initialized with the value 20, you can access any elements of the array. The elements of an array are accessed by their indices.The index of an array in C# starts from zero. Therefore, the first element of an array has an index zero. To assign a value 100 to the last element of the array Integer, use the following statement:

Integer [19] = 100;

Similar to integers, you can use arrays to store character values. An array of character values is called a string. Strings will now be discussed in detail.