C# Array Class
The Array class is the base class for all arrays in C# and is defined in the System namespace. The Array class provides various properties and methods for arrays.
Array Class Properties
The following table lists some of the most commonly used properties of the Array class:
Number | Property & Description |
---|---|
1 | IsFixedSize <br>Gets a value indicating whether the array has a fixed size. |
2 | IsReadOnly <br>Gets a value indicating whether the array is read-only. |
3 | Length <br>Gets a 32-bit integer that represents the total number of elements in all the dimensions of the array. |
4 | LongLength <br>Gets a 64-bit integer that represents the total number of elements in all the dimensions of the array. |
5 | Rank <br>Gets the rank (number of dimensions) of the array. |
For a complete list of properties of the Array class, refer to the Microsoft C# documentation.
Array Class Methods
The following table lists some of the most commonly used methods of the Array class:
Number | Method & Description |
---|---|
1 | Clear <br>Sets a range of elements in the array to zero, false, or null, depending on the element type. |
2 | Copy(Array, Array, Int32) <br>Copies a range of elements from an array starting at the first element and pastes them into another array starting at the first element. The length is specified as a 32-bit integer. |
3 | CopyTo(Array, Int32) <br>Copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index. The index is specified as a 32-bit integer. |
4 | GetLength <br>Gets a 32-bit integer that represents the number of elements in the specified dimension of the array. |
5 | GetLongLength <br>Gets a 64-bit integer that represents the number of elements in the specified dimension of the array. |
6 | GetLowerBound <br>Gets the lower bound of the specified dimension in the array. |
7 | GetType <br>Gets the type of the current instance. Inherited from Object. |
8 | GetUpperBound <br>Gets the upper bound of the specified dimension in the array. |
9 | GetValue(Int32) <br>Gets the value at the specified position in the one-dimensional array. The index is specified as a 32-bit integer. |
10 | IndexOf(Array, Object) <br>Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional array. |
11 | Reverse(Array) <br>Reverses the sequence of the elements in the entire one-dimensional array. |
12 | SetValue(Object, Int32) <br>Sets a value to the element at the specified position in the one-dimensional array. The index is specified as a 32-bit integer. |
13 | Sort(Array) <br>Sorts the elements in the entire one-dimensional array using the IComparable implementation of each element of the array. |
14 | ToString <br>Returns a string that represents the current object. Inherited from Object. |
For a complete list of methods of the Array class, refer to the Microsoft C# documentation.
Example
The following program demonstrates the usage of some methods of the Array class:
using System;
namespace ArrayApplication
{
class MyArray
{
static void Main(string[] args)
{
int[] list = { 34, 72, 13, 44, 25, 30, 10 };
Console.Write("Original Array: ");
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine();
// Reverse the array
Array.Reverse(list);
Console.Write("Reversed Array: ");
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine();
// Sort the array
Array.Sort(list);
Console.Write("Sorted Array: ");
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
}
}
Console.Write("Sorted array: "); foreach (int i in list) { Console.Write(i + " "); } Console.WriteLine();
Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following result:
Original array: 34 72 13 44 25 30 10
Reversed array: 10 30 25 44 13 72 34
Sorted array: 10 13 25 30 34 44 72