
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# - Array Class
An array is a collection of elements of the same data types in a contiguous memory location. In C#, the Array class provides a variety of methods and properties for creating, manipulating, searching, and sorting arrays −
Properties of the Array Class
The following table describes some of the most commonly used properties of C# Array class −
Sr.No. | Property & 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 in all the dimensions of the Array. |
4 |
LongLength Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array. |
5 |
Rank Gets the rank (number of dimensions) of the Array. |
Methods of the Array Class
The following table describes some of the most commonly used methods of the Array class −
Sr.No. | Methods & Description |
---|---|
1 |
AsReadOnly<T>():
It returns a read-only wrapper for the specified array. |
2 |
BinarySearch()
It searches a sorted array for a value. |
3 |
Clear()
It sets a range of elements in the Array to zero, to false, or to null, depending on the element type. |
4 |
Clone()
It creates a shallow copy of the array. |
5 |
ConstrainedCopy()
It copies a range of elements from one array to another. |
6 |
ConvertAll<TInput,TOutput>()
It converts an array of one type to another type using a specified converter |
7 |
CopyTo()
It copies all elements of the array to another array, starting at a specified index. |
8 |
Copy()
It copies a range of elements in one array to another and performs typecasting and boxing. |
9 |
CreateInstance()
It creates an array of a specified type and dimension lengths. |
10 |
Empty<T>()
It returns an empty array of type T. |
11 |
Exists<T>()
It determines whether the array contains elements. |
12 |
Fill<T>()
It fills the entire array with the specified value. |
13 |
FindIndex<T>()
It returns an index defined by the specified predicate. |
14 |
Find<T>()
It searches for an element that matches the conditions. |
15 |
ForEach<T>()
It performs the specified action on each element of the array. |
16 |
GetLength()
It gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.. |
17 |
GetUpperBound()
It gets the index of the last element of the specified dimension in the array. |
18 |
IndexOf()
It searches for a specified element in the array and returns its index. |
19 |
LastIndexOf()
It return the index of the last occurrence of specified element. |
20 |
Resize<T>()
It changes the size of a one-dimensional array to a specified size. |
21 |
Reverse()
It reverses the sequence of elements in the entire array or a subset of it. |
22 |
SetValue()
It sets a value to a specific element in an array at a given position. |
23 |
Sort()
It sorts the element in the array. |
24 |
TrueForAll<T>()
It checks whether all elements in array match the condition. |
In this chapter, we have summarized key methods that are commonly used in C# programming. For complete list of Array class properties and methods, please visit the Microsoft documentation on C# −
Example C# Array Class Methods
The following program demonstrates use of some of the 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 }; int[] temp = list; Console.Write("Original Array: "); foreach (int i in list) { Console.Write(i + " "); } Console.WriteLine(); // reverse the array Array.Reverse(temp); Console.Write("Reversed Array: "); foreach (int i in temp) { 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.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