C# Array class
To work with the various array related operations, an Array class is facilitated by C#. For creating, manipulating, searching, and sorting elements of an array, the Array class in C# provides several methods and properties. For all arrays in the .NET programming environment, the C# class works as the base class. Being not a part of a collection, but being based on the IList interface, an array in C# is yet considered as a collection.
C# Array class Signature:
[SerializableAttribute] [ComVisibleAttribute(true)] public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
C# Array Properties:
The various properties used in an array in C# are listed below:
Property | Uses |
IsFixedSize | To return a value specifying whether an Array has a fixed size or not. |
IsReadOnly | To determine whether an Array is read-only or not. |
IsSynchronized | To determine whether an Array is synchronized or not. |
Length | To return the total number of elements in an Array. |
LongLength | To return a 64-bit integer representing the total count of elements in an Array. |
Rank | To return the number of dimensions or rank of an Array. |
SyncRoot | To return an object to synchronize access to the Array. |
C# Array Methods:
The various methods used in an array in C# are listed below:
Method | Uses |
AsReadOnly<T>(T[]) | To get a read-only wrapper for the specified array. |
BinarySearch(Array,Int32,Int32,Object) | To search for a range of elements for a value in a single-dimensional sorted array. |
BinarySearch(Array,Object) | To search for a specific element in an entire single-dimensional sorted array. |
Clear(Array,Int32,Int32) | To set a range of elements to the default value in an array. |
Clone() | To create a shallow copy of an Array. |
Copy(Array,Array,Int32) | To copy elements of an array to a second array by specifying the starting index. |
CopyTo(Array,Int32) | To copy all the elements of a single-dimensional array to other defined single-dimensional array starting at the defined destination array index. |
CreateInstance(Type,Int32) | To create a single-dimensional Array of the defined Type and length. |
Empty<T>() | To return an empty array. |
Finalize() | To free resources and perform cleanup operations. |
Find<T>(T[],Predicate<T>) | To search for an element matching the conditions specified by the indicated predicate. |
IndexOf(Array,Object) | To search for the defined object and to get the index of its first occurrence in a single-dimensional array. |
Initialize() | To initialize each element of the value-type Array. For doing so it calls the default constructor of the value type. |
Reverse(Array) | To reverse the sequence of the elements in an entire single-dimensional Array. |
Sort(Array) | To sort the elements in an entire single-dimensional Array. |
ToString() | To return a string representing the current object. |
Example:
using System; namespace array_class { class Example { static void Main(string[] args) { // Creating an array int[] a = new int[5] { 5, 10, 15, 20, 25}; // Creating an empty array int[] a2 = new int[5]; // Displaying length of array Console.WriteLine("Length of First array: "+ a.Length); // Sorting array Array.Sort(a); Console.Write("Elements of First array: "); // Displaying sorted array Display(a); // Finding index of an array element Console.WriteLine("\nIndex position of 15: "+ Array.IndexOf(a, 15)); // Coping first array to empty array Array.Copy(a, a2, a.Length); Console.Write("Elements of Second array: "); // Displaying second array Display(a2); Array.Reverse(a); Console.Write("\nElements of First array in reverse order: "); Display(a); } static void Display(int[] a) { foreach (Object x in a) { Console.Write(x +" "); } } } } |
Output:
Explanation:
In the above example, we are displaying the use of the Length property of the C# array class and various methods of an array class, such as IndexOf(Array,Object), Reverse(Array), Sort(Array), and Copy(Array, Array, Int32).