C # Array class
Array
class is the base class for all arrays in C #. It is defined in the System
, which is defined in the namespace. Array
class provides a variety of properties and methods for arrays.
Properties of the Array class
The following table lists some of the most commonly used properties in the Array
:
Serial number |
Attribute & description |
---|---|
1 |
IsFixedSize gets a value indicating whether the array has a fixed size. |
2 |
IsReadOnly gets a value indicating whether the array is read-only. |
3 |
Length gets a 32-bit integer that represents the total number of elements inthe array of all dimensions. |
4 |
LongLength gets a 64-bit integer that represents the total number of elements in the array of all dimensions. |
5 |
Rank gets the rank (dimension) of the array. |
If you need to know Array
for a complete list of properties of the class, please refer to Microsoft’s C # documentation.
Methods of the Array class
The following table lists some of the most commonly used methods in the Array
:
Serial number |
Method & description |
---|---|
1 |
Clear sets a range of elements in the array to zero, false, or null, depending on the type of element. |
2 |
Copy (Array, Array, Int32) copies a range of elements from the first elementof the array to the first element position of another array. The length is specified by a 32-bit integer. |
3 |
CopyTo (Array, Int32) copies all elements from the current one-dimensional array to the specified index location of a specified one-dimensional array. The index is specified by a 32-bit integer. |
4 |
GetLength gets a 32-bit integer that represents the total number of elementsin the array of the specified dimension. |
5 |
GetLongLength gets a 64-bit integer that represents the total number of elements in the array of the specified dimension. |
6 |
GetLowerBound gets the lower bound of the specified dimension in the array. |
7 |
GetType gets the type of the current instance. Inherits from objects. |
8 |
GetUpperBound gets the upper bound of the specified dimension in the array. |
9 |
GetValue (Int32) gets the value of the specified position in an one - dimensional array. The index is specified by a 32-bit integer. |
10 |
IndexOf (Array, Object) searches for the specified object and returns the index that first appears in the entire one-dimensional array. |
11 |
Reverse (Array) reverses the order of elements in the entire one-dimensionalarray. |
12 |
SetValue (Object, Int32) sets a value for the element at the specified position in the one-dimensional array. The index is specified by a 32-bit integer. |
13 |
Sort (Array) uses the IComparable implementation of each element of the array to sort the elements in the entire one-dimensional array. |
14 |
ToString returns a string that represents the current object. Inherits from objects. |
If you need to know Array
for a complete list of methods for the class,please refer to Microsoft’s C # documentation.
Example
The following program demonstrates the use of some methods of Array
:
Example
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 Array
Array.Reverse(list);
Console.Write("Reverse Array: ");
foreach (int i in list)
{
Console.Write(i + " ");
}
Console.WriteLine();
// Sort Array
Array.Sort(list);
Console.Write("Sort 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 results:
Original array: 34 72 13 44 25 30 10
Reverse array: 10 30 25 44 13 72 34
Sort array: 10 13 25 30 34 44 72