The Array class in C# belongs to the System namespace and provides built-in properties and methods for working with arrays. It supports common operations such as sorting, searching, copying, reversing, and retrieving information about array elements.
- The Array class is the base class for all array types in C#.
- It provides properties such as Length, LongLength, and Rank to retrieve array information.
- It includes methods such as Sort(), Reverse(), Copy(), and BinarySearch() to perform operations on arrays.
Example: The Length property returns the total number of elements in an array.
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 its 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;
Example:
Array numbers = new int[] { 10, 20, 30 };
The variable numbers refers to an integer array through the Array base class. For direct access to integer elements, int[] is generally preferred because it preserves the element type.
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. |
1. Length Property
The Length property returns the total number of elements in an array, including all dimensions in the case of multidimensional arrays.
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
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.
Advantages of Array Class in C#
- Built-in Operations: Provides methods for sorting, searching, copying, and reversing arrays.
- Code Reusability: Eliminates the need to write custom logic for common array operations.
- Array Information: Provides properties to retrieve array length, dimensions, and other details.
- Improved Readability: Makes array operations simpler and easier to maintain.
Limitations of Array Class in C#
- Fixed Array Size: Array objects cannot be resized after creation. Array.Resize() creates a replacement array when necessary.
- Sorting Requirements: Methods such as BinarySearch() require the array to be sorted using compatible comparison rules.
- Type Compatibility: Sorting and searching operations may require elements that support comparison or a suitable comparer.
- Limited Dynamic Operations: Arrays do not directly support adding or removing elements. List<T> is generally preferable for dynamically sized collections.