□ Using the different collection classes (such as Dictionary, Stacks, and Queue) in .NET
Arrays
Anint
, string
, and decimal
, respectively:
int[] num;
string[] sentences;
decimal[] values;
Array variables are actually objects. In this example, num
, sentences
, and values
are objects of type System.Array
.
These statements simply declare the three variables as arrays; the variables are not initialized yet, and at this stage you do not know how many elements are contained within each array.
To initialize an array, use the new
keyword. The following statements declare and initialize three arrays:
int[] num = new int[5];
string[] sentences = new string[3];
decimal[] values = new decimal[4];
The num
array now has five members, while the sentences
array has three members, and the values
array has four. The[]
) indicates the number of elements contained in each array.
You can declare an array and initialize it separately, as the following statements show:
//---declare the arrays---
int[] num;
string[] sentences;
decimal[] values;
//---initialize the arrays with default values---
num = new int[5];
sentences = new string[3];
values = new decimal[4];
When you declare an array using the new keyword, each member of the array is initialized with the default value of the type. For example, the preceding num
array contains elements of value 0. Likewise, for the sentences
string array, each of its members has the default value of null.
To learn the default value of a value type, use the default keyword, like this:
object x;
x = default(int); //---0---
x = default(char); //---0 ' '---
x = default(bool); //---false---
To initialize the array to some value other than the default, you use an initialization list. The number of elements it includes must match the array's rank specifier. Here's an example:
int[] num = new int[5] { 1, 2, 3, 4, 5 };
string[] sentences = new string[3] {
'C#', 'Programmers', 'Reference'
};
decimal[] values = new decimal[4] {1.5M, 2.3M, 0.3M, 5.9M};
Because the initialization list already contains the exact number of elements in the array, the rank specifier can be omitted, like this:
int[] num = new int[] { 1, 2, 3, 4, 5 };
string[] sentences = new string[] {
'C#', 'Programmers', 'Reference'
};
decimal[] values = new decimal[] {1.5M, 2.3M, 0.3M, 5.9M};
Use the new var
keyword in C# to declare an implicitly typed array:
var num = new [] { 1, 2, 3, 4, 5 };
var sentences = new [] {
'C#', 'Programmers', 'Reference'
};
var values = new [] {1.5M, 2.3M, 0.3M, 5.9M};
For more information on the var
keyword, see Chapter 3.
In C#, arrays all derive from the abstract base class Array
(in the System namespace) and have access to all the properties and methods contained in that. In Figure 13-1 IntelliSense shows some of the properties and methods exposed by the num
array.

Figure 13-1
That means you can use the Rank
property to learn the dimension of an array. To find out how many elements are contained within an array, you can use the Length
property. The following statements produce the output shown in Figure 13-2.
Console.WriteLine('Dimension of num is {0}', num.Rank);
Console.WriteLine('Number of elements in num is {0}', num.Length);

Figure 13-2
To sort an array, you can use the static Sort()
method in the Array
class:
int[] num = new int[] { 5, 3, 1, 2, 4 };
foreach (int i in num) Console.WriteLine(i);
These statements print out the array in sorted order:
1
2
3
4
5
Accessing Array Elements
To access an element in an array, you specify its index, as shown in the following statements: