C# Array class gives methods for creating, manipulating, searching, and sorting arrays. Array class will be not part of the System.Collections namespace, but it will be still considered as a collection because it is based on the IList interface. The Array class is the base class for language implementations that support arrays.
Characteristics of Array Class:
- In Array, the elements are the value of the array and the length of the array is the total number of item present in the array.
- The lower bound of an Array is the index of its first element and the default value of the lower bound is 0.
- The default size of an Array is 2GB.
- Array objects with the same array type share the same Type object.
Example:
// C# program to creating an array // of the string as coffee name, store // coffee name in the store, // and prints each value. using System; namespace techfortech { class TECH { // Main Method public static void Main() { // declares an 1D Array of string string[] store; // allocating memory for coffee names. store = new string[] {"Americano, ", "Cafe au lait, ", "Espresso, ", "Cappuccino, ", "Long Black, ", "Macchiato" }; // Displaying Elements of the array Console.WriteLine("Different types of coffee: "); Console.WriteLine(); foreach(string coffeename in store) Console.WriteLine(coffeename + " "); } } }
Output:
Different types of coffee: Americano, Cafe au lait, Espresso, Cappuccino, Long Black, Macchiato
Properties
Property | Description |
---|---|
IsFixedSize | Gets a value indicating whether the Array has a fixed size. |
IsReadOnly | Gets a value indicating whether the Array is read-only. |
IsSynchronized | Gets a value indicating whether access to the Array is synchronized (thread safe). |
Length | Gets the total number of elements in all the dimensions of the Array. |
LongLength | Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array. |
Rank | Gets the rank (number of dimensions) of the Array. For example, a one-dimensional array returns 1, a two-dimensional array returns 2, and so on. |
SyncRoot | Gets an object that can be used to synchronize access to the Array. |
Example 1:
// C# program to illustrate // Length property of Array class using System; namespace techfortech { class TECH { // Main Method public static void Main() { // declares an 1D Array of string. string[] topic; // allocating memory for topic. topic = new string[] {"Array, ", "String, ", "Stack, ", "Queue, ", "Exception, ", "Operators"}; // Displaying Elements of the array Console.WriteLine("Topic of C#:"); Console.WriteLine(); // Here we calculate and print // the length of the array, i.e. 6 Console.WriteLine("Length of the array: {0}", topic.Length); foreach(string ele in topic) Console.WriteLine(ele + " "); } } }
Output:
Topic of C#: Length of the array: 6 Array, String, Stack, Queue, Exception, Operators
Example 2:
// C# program to illustrate the // Rank property of Array class using System; namespace techfortech { class TECH { // Main Method public static void Main() { // declares an 1D Array of string. string[] topic; // allocating memory for topic. topic = new string[] {"Array, ", "String, ", "Stack, ", "Queue, ", "Exception, ", "Operators" }; // Displaying Elements of array Console.WriteLine("Topic of C#:"); Console.WriteLine(); // Rank property provides the dimension rank // here we use 1-D array so it return 1 // if we use 2-D array then it will return 2 Console.WriteLine("Rank of the array: {0}", topic.Rank); foreach(string ele in topic) Console.WriteLine(ele + " "); } } }
Output:
Topic of C#: Rank of the array: 1 Array, String, Stack, Queue, Exception, Operators
Method
Method | Description |
---|---|
AsReadOnly() | Returns a read-only wrapper for the specified array. |
BinarySearch() | Searches a one-dimensional sorted Array for a value, using a binary search algorithm. |
Clear() | Sets a range of elements in an array to the default value of each element type. |
Clone() | Creates a shallow copy of the Array. |
ConstrainedCopy() | Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely. |
ConvertAll() | Converts an array of one type to an array of another type. |
Copy() | Copies a range of elements in one Array to another Array and performs type casting and boxing as required. |
CopyTo() | Copies all the elements of the current one-dimensional array to the specified one-dimensional array. |
CreateInstance() | Initializes a new instance of the Array class. |
Empty() | Returns an empty array. |
Equals() | Determines whether the specified object is equal to the current object. |
Exists() | Determines whether the specified array contains elements that match the conditions defined by the specified predicate. |
Find() | Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array. |
FindAll() | Retrieves all the elements that match the conditions defined by the specified predicate. |
FindIndex() | Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within an Array or a portion of it. |
FindLast() | Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire Array. |
FindLastIndex() | Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the last occurrence within an Array or a portion of it. |
ForEach() | Performs the specified action on each element of the specified array. |
GetEnumerator() | Returns an IEnumerator for the Array. |
GetHashCode() | Serves as the default hash function. |
GetLength() | Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array. |
GetLongLength() | Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array. |
GetLowerBound() | Gets the index of the first element of the specified dimension in the array. |
GetType() | Gets the Type of the current instance. |
GetUpperBound() | Gets the index of the last element of the specified dimension in the array. |
GetValue() | Gets the value of the specified element in the current Array. |
IndexOf() | Searches for the specified object and returns the index of its first occurrence in a one-dimensional array or in a range of elements in the array. |
Initialize() | Initializes every element of the value-type Array by calling the default constructor of the value type. |
LastIndexOf() | Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
Resize() | Changes the number of elements of a one-dimensional array to the specified new size. |
Reverse() | Reverses the order of the elements in a one-dimensional Array or in a portion of the Array. |
SetValue() | Sets the specified element in the current Array to the specified value. |
Sort() | Sorts the elements in a one-dimensional array. |
ToString() | Returns a string that represents the current object. (Inherited from Object) |
TrueForAll() | Determines whether every element in the array matches the conditions defined by the specified predicate. |
Example 1:
// C# program to illustrate the Reverse() Method using System; namespace techfortech { class TECH { // Main Method public static void Main() { // declares an 1D Array of string. string[] topic; // allocating memory for topic. topic = new string[] {"Array, ", "String, ", "Stack, ", "Queue, ", "Exception, ", "Operators" }; // Displaying Elements of // the array before reverse Console.WriteLine("Topic of C# before reverse:"); Console.WriteLine(); foreach(string ele in topic) { Console.WriteLine(ele + " "); } Console.WriteLine(); // using Reverse() method to // reverse the given array Array.Reverse(topic); // Displaying Elements of array after reverse Console.WriteLine("Topic of C# after reverse:"); Console.WriteLine(); foreach(string val in topic) { Console.WriteLine(val + " "); } } } }
Output:
Topic of C# before reverse: Array, String, Stack, Queue, Exception, Operators Topic of C# after reverse: Operators Exception, Queue, Stack, String, Array,
Example 2:
// C# program to illustrate the Sort() Method using System; namespace techfortech { class TECH { // Main Method public static void Main() { // declares an 1D Array of string. string[] topic; // allocating memory for topic. topic = new string[] {"Array, ", "String, ", "Stack, ", "Queue, ", "Exception, ", "Operators" }; // Displaying Elements of the array before sort Console.WriteLine("Topic of C# before reverse:"); Console.WriteLine(); foreach(string ele in topic) { Console.WriteLine(ele + " "); } Console.WriteLine(); // using Sort() method to // sort the given array Array.Sort(topic); // Displaying Elements of // array after sort Console.WriteLine("Topic of C# after reverse:"); Console.WriteLine(); foreach(string val in topic) { Console.WriteLine(val + " "); } } } }
Output:
Topic of C# before reverse: Array, String, Stack, Queue, Exception, Operators Topic of C# after reverse: Array, Exception, Operators Queue, Stack, String,
Recent Comments