The Array class in C# belongs to the System namespace and provides properties and methods for working with array objects. It supports operations such as searching, sorting, copying, reversing, and retrieving information about arrays.
- Provides properties such as Length, LongLength, and Rank.
- Provides methods such as Sort(), BinarySearch(), Copy(), and Reverse().
- The
Arrayclass is part of theSystemnamespace. - It provides common operations for manipulating and inspecting arrays.
using System;
class Program
{
static void Main()
{
Array numbers = new int[] { 10, 20, 30, 40, 50 };
Console.WriteLine(numbers.Length);
}
}
Output
5
Here, numbers is an Array reference that refers to an integer array, and Length returns the total number of elements.
Declaration of Array Class
The Array class is defined in the System namespace and represents the base class for all arrays in C#. It provides methods and properties for managing arrays.
Syntax:
Array arrayName;
Array Class Properties
The Array class provides properties that return information about an array, such as its size, dimensions, and synchronization status.
| Property | Description |
|---|---|
| IsFixedSize | Indicates whether the array has a fixed size. |
| IsReadOnly | Indicates whether the array is read-only. |
| IsSynchronized | Indicates whether access to the array is synchronized. |
| Length | Gets the total number of elements in all dimensions. |
| LongLength | Gets the total number of elements as a 64-bit integer. |
| Rank | Gets the number of dimensions of the array. |
| SyncRoot | Gets an object that can be used to synchronize access to the array. |
Example 1: Length Property
The Length property returns the total number of elements in an array.
using System;
class Program
{
static void Main()
{
int[] numbers = { 10, 20, 30, 40, 50, 60 };
Console.WriteLine("Length of the array: " + numbers.Length);
}
}
Output
Length of the array: 6
Example 2: Rank Property
The Rank property returns the number of dimensions of an array.
using System;
class Program
{
static void Main()
{
int[] numbers = { 10, 20, 30, 40, 50, 60 };
Console.WriteLine("Rank of the array: " + numbers.Rank);
}
}
Output
Rank of the array: 1
Array Class Methods
The Array class in C# provides static and instance methods for performing common operations on arrays, including searching, sorting, copying, modifying, and retrieving elements. These methods are available through the System.Array class.
| Method | Description |
|---|---|
Sort() | Sorts the elements of an array. |
BinarySearch() | Searches a sorted array using binary search. |
Copy() | Copies elements from one array to another. |
CopyTo() | Copies elements to another array. |
Clear() | Sets a range of elements to their default values. |
Clone() | Creates a shallow copy of the array. |
Reverse() | Reverses the order of elements in an array. |
IndexOf() | Returns the index of the first occurrence of a value. |
LastIndexOf() | Returns the index of the last occurrence of a value. |
Resize() | Changes the size of a one-dimensional array. |
Find() | Returns the first element that matches a condition. |
FindAll() | Returns all elements that match a condition. |
ForEach() | Performs an action on each element of an array. |
Examples of Array Class Methods
1. Array.Reverse()
The Reverse() method reverses the order of elements in an array.
using System;
class Program
{
static void Main()
{
int[] numbers = { 10, 20, 30, 40, 50, 60 };
Console.WriteLine("Array before reversing:");
foreach (int number in numbers)
{
Console.Write(number + " ");
}
Array.Reverse(numbers);
Console.WriteLine("\nArray after reversing:");
foreach (int number in numbers)
{
Console.Write(number + " ");
}
}
}
Output
Array before reversing: 10 20 30 40 50 60 Array after reversing: 60 50 40 30 20 10
Explanation:
- The
numbersarray is initialized with six integer values. - The first
foreachloop displays the elements before reversing. Array.Reverse(numbers)reverses the order of the elements in the array.- The second
foreachloop displays the elements after reversing. - The original array is modified directly by the
Reverse()method.
2. Array.Sort()
The Sort() method sorts the elements of an array in ascending order by default.
using System;
class Program
{
static void Main()
{
int[] numbers = { 50, 20, 40, 10, 60, 30 };
Console.WriteLine("Array before sorting:");
foreach (int number in numbers)
{
Console.Write(number + " ");
}
Array.Sort(numbers);
Console.WriteLine("\nArray after sorting:");
foreach (int number in numbers)
{
Console.Write(number + " ");
}
}
}
Output
Array before sorting: 50 20 40 10 60 30 Array after sorting: 10 20 30 40 50 60
Explanation:
- The numbers array is initialized with six unsorted integer values.
- The first foreach loop displays the elements before sorting.
- Array.Sort(numbers) sorts the elements of the array in ascending order.
- The second foreach loop displays the sorted elements.
- Array.Sort() modifies the original array directly.