15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

134 ❘ ChaPTer 6 ArrAys And tuples<br />

for (int row = 0; row < jagged.Length; row++)<br />

{<br />

for (int element = 0; element < jagged[row].Length; element++)<br />

{<br />

Console.WriteLine("row: {0}, element: {1}, value: {2}",<br />

row, element, jagged[row][element]);<br />

}<br />

}<br />

The outcome of the iteration displays the rows <strong>and</strong> every element within the rows:<br />

row: 0, element: 0, value: 1<br />

row: 0, element: 1, value: 2<br />

row: 1, element: 0, value: 3<br />

row: 1, element: 1, value: 4<br />

row: 1, element: 2, value: 5<br />

row: 1, element: 3, value: 6<br />

row: 1, element: 4, value: 7<br />

row: 1, element: 5, value: 8<br />

row: 2, element: 1, value: 9<br />

row: 2, element: 2, value: 10<br />

row: 2, element: 3, value: 11<br />

array Class<br />

Declaring an array with brackets is a <strong>C#</strong> notation using the Array class. Using the <strong>C#</strong> syntax behind the<br />

scenes creates a new class that derives from the abstract base class Array. This makes it possible to use<br />

methods <strong>and</strong> properties that are defined with the Array class with every <strong>C#</strong> array. For example, you’ve<br />

already used the Length property or iterated through the array by using the foreach statement. By doing<br />

this, you are using the GetEnumerator() method of the Array class.<br />

Other properties that are implemented by the Array class are LongLength for arrays where the number of<br />

items doesn’t fit within an integer, <strong>and</strong> Rank to get the number of dimensions.<br />

Let’s have a look at other members of the Array class by getting into various features.<br />

Creating arrays<br />

The Array class is abstract, so you cannot create an array by using a constructor. However, instead<br />

of using the <strong>C#</strong> syntax to create array instances, it is also possible to create arrays by using the static<br />

CreateInstance() method. This is extremely useful if you don’t know the type of elements in advance,<br />

because the type can be passed to the CreateInstance() method as a Type object.<br />

The following example shows how to create an array of type int with a size of 5. The first argument of the<br />

CreateInstance() method requires the type of the elements, <strong>and</strong> the second argument defines the size.<br />

You can set values with the SetValue() method <strong>and</strong> read values with the GetValue() method.<br />

Array intArray1 = Array.CreateInstance(typeof(int), 5);<br />

for (int i = 0; i < 5; i++)<br />

{<br />

intArray1.SetValue(33, i);<br />

}<br />

for (int i = 0; i < 5; i++)<br />

{<br />

Console.WriteLine(intArray1.GetValue(i));<br />

}<br />

You can also cast the created array to an array declared as int[]:<br />

int[] intArray2 = (int[])intArray1;<br />

code snippet Sample1/Program.cs<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!