.NET Type System, Part II - Single-Dimension Arrays
(Page 4 of 7 )
This is the basic array type. It has only one dimension, and it's the most common type that you will be using in your applications. Here's an example:
using System;
namespace Arrays
{
class SingleDimension
{
static void Main(string[] args)
{
// creating the array
int[] Numbers = new int[5];
// initializing the array
for(int x = 0; x < Numbers.Length; x++)
{
Numbers[x] = (x + x);
}
// printing the array elements to the Console
for(int x = 0; x < Numbers.Length; x++)
{
Console.WriteLine("Element number {0} = {1}",x, Numbers[x]);
}
Console.ReadLine();
}
}
}
The output of the program will be:
Element number 0 = 0
Element number 1 = 2
Element number 2 = 4
Element number 3 = 6
Element number 4 = 8
This is a very simple example of a single-dimension array. We first create an array of five integer elements, then we loop on the elements to initialize them, and then we do another loop to write them to the Console. Note that we use the Length property of the array (inherited from the base class System.Array) to return the number of elements in the array.
Let's extend this example. We will not print the element's value in the next example; instead, we will copy the values into a new array using the static method Clone of the base class. Note that this method performs a shallow copying, not a deep copying -- in other words, it will copy value types exactly as you expect (copying the value of the element to the target array's element), but with reference types it will copy the reference itself, not the object that the reference refers to. So, for example, it will copy the reference aCustomer, not the member fields FirstName and LastName to the new target array's element.
using System;
namespace Arrays
{
class SingleDimension
{
static void Main(string[] args)
{
// creating the array
int[] Numbers = new int[5];
// initializing the array
for(int x = 0; x < Numbers.Length; x++)
{
Numbers[x] = (x + x);
}
// copying the array into a new array
int[] NewNumbers = (int[])Numbers.Clone();
//printing the values of the new array
for(int x = 0; x < NewNumbers.Length; x++)
{
Console.WriteLine("NewNumbers's element number {0} = {1}",x, NewNumbers[x]);
}
Console.ReadLine();
}
}
}
We used the Clone() method to copy the values to the newly created array NewNumbers. Note that we must cast to the target array type, because the return type of the clone method is object[] not int[]. We will discuss type conversion in later articles. The result of this code is:
NewNumber's element number 0 = 0
NewNumber's element number 1 = 2
NewNumber's element number 2 = 4
NewNumber's element number 3 = 6
NewNumber's element number 4 = 8
Let's create another example that sorts an array. Copy the following code into a file with the .cs extension, then compile it:
using System;
namespace Arrays
{
class SingleDimension
{
static void Main(string[] args)
{
int[] Numbers = {6,3,10,35,2};
foreach(int i in Numbers)
{
Console.WriteLine(i);
}
Array.Sort(Numbers);
Console.WriteLine("After sorting operation");
foreach(int i in Numbers)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
We have used the Array.Sort() method that accepts an array and sorts it; after that we used the foreach structure, which we will discuss later in the series, to iterate through the array and print the value of the elements.
Next: Multi-Dimension Arrays >>
More .NET Articles
More By Michael Youssef