Wednesday, December 07, 2011

Arrays 3

Class System.Array is the base class for all arrays.

Length property gets the total number of elements in the Array

GetValue property gets element value in the given index

SetValue property sets a given element value in the index specified in the second parameter of that method

class Program
{
    static void Main(string[] args)
    {
        int[] a = new int[5];
        System.Array o = a;
        Console.WriteLine("Array Length:" + a.Length.ToString());
        // Assign element values
        for (int i = 0; i < o.Length; i++)
        {
            int intValue = i * i;
            o.SetValue(intValue, i);
        }
        // Retrieve element values
        for (int i = 0; i < o.Length; i++)
        {
            Console.WriteLine("Element " + (i + 1).ToString() + ":" + o.GetValue(i));
        }
        Console.ReadKey();
    }
}

No comments:

Post a Comment