Arrays as Objects

In C#, arrays are actually objects.

System.Array is the abstract base type of all array types.

You can use its properties, and other members.

The following code assigns the length of the numbers array, which is 5, to a variable called lengthOfNumbers:


using System;

public class MainClass{
   public static void Main(){
      int[] numbers = { 1, 2, 3, 4, 5 };
      int lengthOfNumbers = numbers.Length;
      Console.WriteLine(lengthOfNumbers);      
   }
}

The output:


5

The System.Array class provides many other useful methods and properties for sorting, searching, and copying arrays.

This example uses the Rank property to display the number of dimensions of an array.


class MainClass
{
    static void Main()
    {
        // Declare and initialize an array:
        int[,] theArray = new int[5, 10];
        System.Console.WriteLine("The array has {0} dimensions.", theArray.Rank);
    }
}

The output:


The array has 2 dimensions.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.