C# Array class

HTML
CSS
C#
SQL

Array class in C#

In C# provides an Array class to deal with array related operations. It provides methods for creating, manipulating, searching, and sorting elements of an array. This class works as the base class for all arrays in the .NET programming environment.

Array Properties

Array methods

Examples:

AsReadOnly(): Returns a read-only wrapper for the specified array.

int[] array = { 1, 2, 3, 4, 5 };

IList<int> readOnlyArray = Array.AsReadOnly(array);

 

BinarySearch(): Searches a sorted one-dimensional array for a specific element, using a binary search algorithm.

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int index = Array.BinarySearch(array, 5); // index will be 4

 

Clear(): Sets a range of elements in the array to zero, false, or null, depending on the element type.

int[] array = { 1, 2, 3, 4, 5 };

Array.Clear(array, 0, array.Length);

 

Clone(): Creates a shallow copy of the Array.

int[] array = { 1, 2, 3, 4, 5 };

int[] cloneArray = (int[])array.Clone();

 

ConstrainedCopy(): Copies a range of elements from one array to another.

int[] sourceArray = { 1, 2, 3, 4, 5 };

int[] destinationArray = new int[5];

Array.ConstrainedCopy(sourceArray, 0, destinationArray, 0, 5);

 

ConvertAll(): Converts an array of one type to an array of another type.

string[] stringArray = { “1”, “2”, “3”, “4”, “5” };

int[] intArray = Array.ConvertAll(stringArray, int.Parse);

 

Copy(): Copies a range of elements from one array to another.

int[] sourceArray = { 1, 2, 3, 4, 5 };

int[] destinationArray = new int[5];

Array.Copy(sourceArray, destinationArray, 5);

 

CopyTo(): Copies all the elements of the current one-dimensional array to the specified one-dimensional array.

int[] sourceArray = { 1, 2, 3, 4, 5 };

int[] destinationArray = new int[5];

sourceArray.CopyTo(destinationArray, 0);

 

CreateInstance(): Creates a new array instance with the specified element type and dimensions.

Array array1 = Array.CreateInstance(typeof(int), 5);

Array array2 = Array.CreateInstance(typeof(string), 2, 3);

 

Empty(): Represents an empty array.

Array emptyArray = Array.Empty<int>();

 

Equals(): Determines whether the specified object is equal to the current object.

int[] array1 = { 1, 2, 3 };

int[] array2 = { 1, 2, 3 };

bool equal = Array.Equals(array1, array2); // equal will be true

 

Exists(): Determines whether the specified array contains elements that match the conditions defined by the specified predicate.

int[] array = { 1, 2, 3, 4, 5 };

bool exists = Array.Exists(array, element => element > 3); // exists will be true

 

Find(): Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire array.

int[] array = { 1, 2, 3, 4, 5 };

int foundElement = Array.Find(array, element => element > 3); // foundElement will be 4

 

FindAll(): Retrieves all the elements that match the conditions defined by the specified predicate.

int[] array = { 1, 2, 3, 4, 5 };

List<int> foundElements = Array.FindAll(array, element => element > 2); // foundElements will contain { 3, 4, 5 }

 

FindIndex(): Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire array.

int[] array = { 1, 2, 3, 4, 5 };

int index = Array.FindIndex(array, element => element > 2); // index will be 2

 

FindLast(): Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire array.

int[] array = { 1, 2, 3, 4, 5 };

int lastFoundElement = Array.FindLast(array, element => element > 2); // lastFoundElement will be 5

 

FindLastIndex(): Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire array.

int[] array = { 1, 2, 3, 4, 5 };

int lastIndex = Array.FindLastIndex(array, element => element > 2); // lastIndex will be 4

 

ForEach(): Performs the specified action on each element of the array.

int[] array = { 1, 2, 3, 4, 5 };

Array.ForEach(array, element => Console.WriteLine(element));

 

GetEnumerator(): Returns an enumerator for the entire array.

int[] array = { 1, 2, 3, 4, 5 };

IEnumerator enumerator = array.GetEnumerator();

 

GetHashCode(): Serves as the default hash function.

int[] array = { 1, 2, 3, 4, 5 };

int hashCode = array.GetHashCode();

 

GetLength(): Gets the length of the specified dimension in the array.

int[] array = { 1, 2, 3, 4, 5 };

int length = array.GetLength(0); // length will be 5

 

GetLongLength(): Gets the total number of elements in all the dimensions of the Array.

int[,] array = new int[3, 4];

long longLength = array.GetLongLength(0); // longLength will be 3

Note: These methods are just the tip of the iceberg – the Array class offers a rich set of tools for manipulating arrays efficiently. Explore the documentation for the Array class and experiment with its various methods.

Practices Tasks

1. Reverse method

For example : First, you declare an array of characters like this char[] characters = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ }; Then, you call the Array.Reverse() method and pass the array you want to reverse as an argument

OUTPUT 

2.Copy method

For example : Declare sourceArray with elements {1, 2, 3, 4, 5} and destinationArray with a size of 5. Then use Array.Copy(sourceArray, destinationArray, 5) to copy elements from sourceArray to destinationArray. This will result in destinationArray holding {1, 2, 3, 4, 5}.show destinationArray in console

OUTPUT 

3. Find method

For example : First, create an array of integers named numbers with the values { 1, 2, 3, 4, 5 }.Then, use Array.Find() method with two arguments:The first argument is the array numbers that you want to search. The second argument is to find the first even number in array  and both find by using a lambda expression that defines the condition to search for. In this case, num => num % 2 == 0 is used, which means it searches for the first even number in the array.

OUTPUT 

4.Foreach method

For example : Declare an array of integers with the named numbers with the values { 1, 2, 3, 4, 5 }.Utilize the Array.ForEach() method to iterate over each element in the numbers array. Inside the Array.ForEach() method, use a lambda expression num => Console.WriteLine(num) to print each number to the console.

OUTPUT