There are various operations that can be done with an array such as creating, manipulating, sorting, searching, etc. This is done by the array class that provides various methods to perform mentioned operation in C#.
Note: In C#, Array is not part of the collection but considered as a collection because it is based on the IList interface.
Properties of the Array Class – C#
The following table represents some of the properties of the Array class that are mostly used.
Property | Description |
---|---|
IsFixedSize | This property is used to get a value indicating whether the Array has a fixed size or not. |
IsReadOnly | This is used to check whether the Array is read-only or not. |
Length | This property gets (32-bit integer) that represents the total number of elements in all the dimensions of the Array. |
LongLength | This property gets (a 64-bit integer) that represents the total number of elements in all the dimensions of the Array. |
Rank | This property gets the rank (number of dimensions) of the Array. |
SyncRoot | It gets an object that can be used to synchronize access to the Array. |
IsSynchronized | It checks whether the access to the Array is synchronized. |
Methods of the Array Class – C#
The following table represents some of the methods of the Array class that are mostly used.
Method | Description |
---|---|
BinarySearch() | This method is used to search an entire one-dimensional sorted array for a specific value. It uses binary search algorithm |
Clear() | It is used to set a range of elements to zero, to null, or to false, depending on the type of element. |
Empty() | Simply returns an empty array. |
Copy() | This method copies elements of an array into another array by specifying the initial index and performs type casting as required. |
CopyTo() | Copies all the elements of the current one-dimensional array to the specified one-dimensional array. |
Clone() | This method creates a shallow copy of the Array. |
CreateInstance() | Using this method new instance of the Array class is initialized. |
Initialize() | This method initializes every element of the value-type Array by calling the default constructor of the value type. |
IndexOf() | This method searches for the specified object and returns the index of the first occurrence in a one-dimensional array. |
Find() | It is used to search for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire Array. |
Reverse() | This method reverses the sequence of the elements in the entire one-dimensional Array. |
ToString() | It returns a string that represents the current object. |
GetUpperBound() | It is used to get the upper bound of the specified dimension in the Array. |
GetLowerBound() | It is used to get the lower bound of the specified dimension in the Array. |
Sort() | It is used to sort the elements in an entire one-dimensional Array by using the IComparable implementation of each element of the Array.. |
AsReadOnly() | This method returns a read-only wrapper for the specified array. |
C# program for Array Class
No let us see an example to demonstrate some of the methods of Array Class in C# programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | using System; namespace ArrayClass { class Program { //We will call this printing function if we want to print in the main program // User defined method static void DisplayFunc(int[] arr) { foreach(Object elem in arr) { Console.Write(elem + " "); } } static void Main(string[] args) { // Creating an array and initializing it int[] arr = new int[5] { 10, 2, 3, 8, 77 }; // Print length of array Console.WriteLine("Length of an Array: " + arr.Length); // Finding index of an array element using IndexOf() Console.WriteLine("Index of 3 in an array: " + Array.IndexOf(arr, 3)); // Creating an empty array to copy int[] copied_arr = new int[6]; Array.Copy(arr, copied_arr, arr.Length); Console.Write("Copied Array (copied_arr): "); DisplayFunc(copied_arr); // Sorting an array Array.Sort(arr); Console.Write("\nSorted array (arr): "); DisplayFunc(arr); //Reverse an array Array.Reverse(arr); Console.Write("\nFirst Array elements in reverse order: "); DisplayFunc(arr); } } } |
Output:
1 2 3 4 5 | Length of an Array: 5 Index of 3 in an array: 2 Copied Array (copied_arr): 10 2 3 8 77 0 Sorted array (arr): 2 3 8 10 77 First Array elements in reverse order: 77 10 8 3 2 |