Array Class in C#
The Array class in C# (defined in the System namespace) provides methods and properties to work with arrays. Unlike simple array declarations, the Array class gives built in functionality to manipulate arrays (sorting, searching, copying, etc.).
Example: Using Array Class
int[] numbers = { 5, 2, 9, 1, 7 };
// Sort numbers using Array class
Array.Sort(numbers);
Array Class Characteristics
- The length of the array is the number of elements it holds.
- The lower bound of an Array is the index of its first element and the default value of the lower bound is 0.
- The default size of an Array is 2GB.
- Arrays of the same type share the same type or Class object.
Array Traversal
Example: This example demonstrates array traversal.
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
Array Class Properties
The Array class provides several properties to access its state.
| Properties | Description |
|---|---|
| IsFixedSize | Indicates whether the array has a fixed size (always true for arrays). |
| IsReadOnly | Indicates whether the array is read-only. |
| IsSynchronized | Indicates whether access to the array is thread-safe. |
| 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. |
Gets an object that can be used to synchronize access (for thread safety). |
Example 1: Length property
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: Rank property
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 sorted one-dimensional array using binary search. |
| Clear() | Sets elements in a range to their default values. |
| Clone() | Creates a shallow copy of the array. |
| ConstrainedCopy() | Copies a range of elements with rollback if the copy fails. |
| ConvertAll() | Converts an array of one type to another type. |
| Copy() | Copies elements from one array to another (with type casting if needed). |
| CopyTo() | Copies all elements to another one-dimensional array. |
| CreateInstance() | Creates a new instance of the Array class. |
| Empty() | Returns an empty array of a specified type. |
| Equals() | Checks if two arrays are equal. |
| Exists() | Checks if any element matches a given condition. |
| Find() | Returns the first element that matches a condition. |
| FindAll() | Returns all elements that match a condition. |
| FindIndex() | Returns the index of the first element that matches a condition. |
| FindLast() | Returns the last element that matches a condition. |
| FindLastIndex() | Returns the index of the last element that matches a condition. |
| ForEach() | Performs an action on each element of the array. |
| GetEnumerator() | Returns an IEnumerator for the Array. |
| GetHashCode() | Returns the hash code for the array. |
| GetLength() | Gets the number of elements in a specified dimension (32-bit). |
| GetLongLength() | Gets the number of elements in a specified dimension (64-bit). |
| GetLowerBound() | Gets the index of the first element of a dimension. |
| GetType() | Gets the runtime type of the array. |
| GetUpperBound() | Gets the index of the last element of a dimension. |
| GetValue() | Gets the value of the specified element in the current Array. |
| IndexOf() | Returns the index of the first occurrence of a value. |
| Initialize() | Initializes each element of a value-type array. |
| LastIndexOf() | Returns the index of the last occurrence of a value. |
| MemberwiseClone() | Creates a shallow copy of the object. |
| Resize() | Resizes a one-dimensional array to the specified size. |
| Reverse() | Reverses the order of elements in an array. |
| SetValue() | Sets the value of an element at a specified index. |
| Sort() | Sorts the elements in a one-dimensional array. |
| ToString() | Returns a string representation of the array (inherited from Object). |
| TrueForAll() | Checks if all elements match a specified condition. |
Example 1: Array.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: Array.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