![]() |
VOOZH | about |
The Array class in C# (defined in the System namespace) provides methods and properties to work with arrays. Unlike simple array declarations, the Array class gives built in functionality to manipulate arrays (sorting, searching, copying, etc.).
Example: Using Array Class
Example: This example demonstrates array traversal.
Array elements are: 10 20 30 40 50
The Array class provides several properties to access its state.
| Properties | Description |
|---|---|
| IsFixedSize | Indicates whether the array has a fixed size (always true for arrays). |
| IsReadOnly | Indicates whether the array is read-only. |
| IsSynchronized | Indicates whether access to the array is thread-safe. |
| Length | Gets the total number of elements in all dimensions. |
| LongLength | Gets the total number of elements as a 64-bit integer. |
| Rank | Gets the number of dimensions of the array. |
Gets an object that can be used to synchronize access (for thread safety). |
Example 1: Length property
Length of the array: 6
Example 2: Rank property
Rank of the array: 1 10 20 30 40 50 60
| Method | Description |
|---|---|
| AsReadOnly() | Returns a read-only wrapper for the specified array. |
| BinarySearch() | Searches a sorted one-dimensional array using binary search. |
| Clear() | Sets elements in a range to their default values. |
| Clone() | Creates a shallow copy of the array. |
| ConstrainedCopy() | Copies a range of elements with rollback if the copy fails. |
| ConvertAll() | Converts an array of one type to another type. |
| Copy() | Copies elements from one array to another (with type casting if needed). |
| CopyTo() | Copies all elements to another one-dimensional array. |
| CreateInstance() | Creates a new instance of the Array class. |
| Empty() | Returns an empty array of a specified type. |
| Equals() | Checks if two arrays are equal. |
| Exists() | Checks if any element matches a given condition. |
| Find() | Returns the first element that matches a condition. |
| FindAll() | Returns all elements that match a condition. |
| FindIndex() | Returns the index of the first element that matches a condition. |
| FindLast() | Returns the last element that matches a condition. |
| FindLastIndex() | Returns the index of the last element that matches a condition. |
| ForEach() | Performs an action on each element of the array. |
| GetEnumerator() | Returns an IEnumerator for the Array. |
| GetHashCode() | Returns the hash code for the array. |
| GetLength() | Gets the number of elements in a specified dimension (32-bit). |
| GetLongLength() | Gets the number of elements in a specified dimension (64-bit). |
| GetLowerBound() | Gets the index of the first element of a dimension. |
| GetType() | Gets the runtime type of the array. |
| GetUpperBound() | Gets the index of the last element of a dimension. |
| GetValue() | Gets the value of the specified element in the current Array. |
| IndexOf() | Returns the index of the first occurrence of a value. |
| Initialize() | Initializes each element of a value-type array. |
| LastIndexOf() | Returns the index of the last occurrence of a value. |
| MemberwiseClone() | Creates a shallow copy of the object. |
| Resize() | Resizes a one-dimensional array to the specified size. |
| Reverse() | Reverses the order of elements in an array. |
| SetValue() | Sets the value of an element at a specified index. |
| Sort() | Sorts the elements in a one-dimensional array. |
| ToString() | Returns a string representation of the array (inherited from Object). |
| TrueForAll() | Checks if all elements match a specified condition. |
Example 1: Array.Reverse()
Array before reverse: 10 20 30 40 50 60 Array after reverse: 60 50 40 30 20 10
Example 2: Array.sort()
Array before sorting: 50 20 40 10 60 30 Array after sorting: 10 20 30 40 50 60