C# Array Class (original) (raw)

Last Updated : 31 Jan, 2025

**Array class in C# is part of the **System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the **System.Collections namespace, but it is still considered as a collection because it is based on the **IList interface. The Array class is the base class for language implementations that support arrays.

**Example: This example demonstrates array traversal.

C# `

// C# program to demonstrates the traversal on array using System;

class Geeks { static void Main() { // Declare and initialize an array of integers int[] arr = { 10, 20, 30, 40, 50 };

    // Print each element of the array
    Console.WriteLine("Array elements are:");
    foreach(int i in arr) { 
      Console.WriteLine(i); 
    }
}

}

`

Output

Array elements are: 10 20 30 40 50

Properties

The Array class provides several properties to access its state.

Properties Description
**IsFixedSize Gets a value indicating whether the Array has a fixed size.
**IsReadOnly Gets a value indicating whether the Array is read-only.
**IsSynchronized Gets a value indicating whether access to the Array is synchronized (thread safe).
**Length Gets the total number of elements in all the dimensions of the Array.
**LongLength Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array.
**Rank Gets the rank (number of dimensions) of the Array. For example, a one-dimensional array returns 1, a two-dimensional array returns 2, and so on.
**SyncRoot Gets an object that can be used to synchronize access to the Array.

**Example 1: This example demonstrates how to use the length property of an array.

C# `

// C# program to demonstrates the length of an array using System;

class Geeks {

public static void Main()
{
    // Declares a 1D array of integers
    int[] arr;

    // Allocating memory for the array
    arr = new int[] { 10, 20, 30, 40, 50, 60 };

    // Displaying the length of the array
    Console.WriteLine("Length of the array: {0}",
                      arr.Length);
}

}

`

Output

Length of the array: 6

**Example 2: This example, demonstrates how to use the Rank property to find the number of dimensions of an array and prints each element of the array.

C# `

// C# program to demonstrates the property rank of an array using System;

class Geeks {

public static void Main()
{
    // Declare and initialize a 1D array of integers
    int[] arr = { 10, 20, 30, 40, 50, 60 };

    // Display the rank (dimension) of the array
    Console.WriteLine("Rank of the array: " + arr.Rank);

    foreach(int i in arr) { 
      Console.WriteLine(i); 
    }
}

}

`

Output

Rank of the array: 1 10 20 30 40 50 60

Methods

Method Description
**AsReadOnly() Returns a read-only wrapper for the specified array.
**BinarySearch() Searches a one-dimensional sorted Array for a value, using a binary search algorithm.
**Clear() Sets a range of elements in an array to the default value of each element type.
**Clone() Creates a shallow copy of the Array.
**ConstrainedCopy() Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely.
**ConvertAll() Converts an array of one type to an array of another type.
**Copy() Copies a range of elements in one Array to another Array and performs type casting and boxing as required.
**CopyTo() Copies all the elements of the current one-dimensional array to the specified one-dimensional array.
**CreateInstance() Initializes a new instance of the Array class.
**Empty() Returns an empty array.
**Equals() Determines whether the specified object is equal to the current object.
**Exists() Determines whether the specified array contains elements that match the conditions defined by the specified predicate.
**Find() Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array.
**FindAll() Retrieves all the elements that match the conditions defined by the specified predicate.
**FindIndex() Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within an Array or a portion of it.
**FindLast() Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire Array.
**FindLastIndex() Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the last occurrence within an Array or a portion of it.
**ForEach() Performs the specified action on each element of the specified array.
**GetEnumerator() Returns an IEnumerator for the Array.
**GetHashCode() Serves as the default hash function.
**GetLength() Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.
**GetLongLength() Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array.
**GetLowerBound() Gets the index of the first element of the specified dimension in the array.
**GetType() Gets the Type of the current instance.
**GetUpperBound() Gets the index of the last element of the specified dimension in the array.
**GetValue() Gets the value of the specified element in the current Array.
**IndexOf() Searches for the specified object and returns the index of its first occurrence in a one-dimensional array or in a range of elements in the array.
**Initialize() Initializes every element of the value-type Array by calling the default constructor of the value type.
**LastIndexOf() Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array.
**MemberwiseClone() Creates a shallow copy of the current Object.
**Resize() Changes the number of elements of a one-dimensional array to the specified new size.
**Reverse() Reverses the order of the elements in a one-dimensional Array or in a portion of the Array.
**SetValue() Sets the specified element in the current Array to the specified value.
**Sort() Sorts the elements in a one-dimensional array.
**ToString() Returns a string that represents the current object. (Inherited from Object)
**TrueForAll() Determines whether every element in the array matches the conditions defined by the specified predicate.

**Example 1: This example demonstrates how to use the Array.Reverse() to reverse the elements of a one-dimensional integer array.

C# `

// C# program to demonstrates the working of Reverse() using System;

class Geeks { public static void Main() { // Declare and initialize a 1D array of integers int[] arr = { 10, 20, 30, 40, 50, 60 };

    // Display the array before reversing
    Console.WriteLine("Array before reverse:");
    foreach(int i in arr) { 
      Console.Write(i + " "); 
    }
    Console.WriteLine();

    // Using Reverse() method to reverse the array
    Array.Reverse(arr);

    // Display the array after reversing
    Console.WriteLine("Array after reverse:");
    foreach(int i in arr) { 
      Console.Write(i + " "); 
    }
    Console.WriteLine();
}

}

`

Output

Array before reverse: 10 20 30 40 50 60 Array after reverse: 60 50 40 30 20 10

**Example 2: This example, demonstrates hoe to use Array.sort() Method to sort a one-dimensional integer array in ascending order.

C# `

// C# program to demonstrates the working of Sort() using System;

class Geeks { public static void Main() { // Declare and initialize a 1D array of integers int[] arr = { 50, 20, 40, 10, 60, 30 };

    // Display the array before sorting
    Console.WriteLine("Array before sorting:");
    foreach(int i in arr) { 
      Console.Write(i + " "); 
    }
    Console.WriteLine();

    // Using Sort() method to sort the array
    Array.Sort(arr);

    // Display the array after sorting
    Console.WriteLine("Array after sorting:");
    foreach(int i in arr) { 
      Console.Write(i + " "); 
    }
    Console.WriteLine();
}

}

`

Output

Array before sorting: 50 20 40 10 60 30 Array after sorting: 10 20 30 40 50 60