What are some of the commonly used methods of the array class in C#?

CsharpProgrammingServer Side Programming

The Array class is the base class for all the arrays in C#. It is defined in the System namespace.The most commonly used methods of the array class are −

Sr.No.Methods & Description
1Clear
Sets a range of elements in the Array to zero, to false, or to null, depending on the element type
2Copy(Array, Array, Int32)
Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.
3CopyTo(Array, Int32)
Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer.
4GetLength
Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.
5GetLongLength
Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array
6GetLowerBound
Gets the lower bound of the specified dimension in the Array.
7GetType
Gets the Type of the current instance. (Inherited from Object.)
8GetUpperBound
Gets the upper bound of the specified dimension in the Array.

Let us see an eample to get the Upperbound as well Lowerbound of an array using Array class methods −

Example

 Live Demo

using System;

class Program {
   static void Main() {
      int[,] arr = new int[3,4];

      Console.WriteLine(arr.GetLength(0));
      Console.WriteLine(arr.GetLength(1));

      // Length
      Console.WriteLine(arr.Length);
      Console.WriteLine("Upper Bound: {0}",arr.GetUpperBound(0).ToString());
      Console.WriteLine("Lower Bound: {0}",arr.GetLowerBound(0).ToString());
   }
}

Output

3
4
12
Upper Bound: 2
Lower Bound: 0
raja
Published on 08-Aug-2018 07:41:22
Advertisements