English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Arrays (Array)

Variables are used to store literal values, while arrays are used to store multiple literal values.

An array is a data structure that stores a fixed number of text values (elements) of the same data type. Array elements are stored continuously in memory.

In C#, arrays can be of three types: one-dimensional arrays, multi-dimensional arrays, and jagged arrays. Here, you will learn about one-dimensional arrays.

The following figure illustrates the array representation form.

Array Representation

Array Declaration and Initialization

You can use square brackets to specify the element type of the array to declare an array.

int[] evenNums;  // Integer array
string[] cities; // String array

The following statements declare and add values to the array in a single statement.

int[] evenNums = new int[5]{ 2, 4, 6, 8, 10 }; 
string[] cities = new string[3]{ "Mumbai", "London", "New York" };

In this case, the evenNums array can store up to five integers. new int[5] The number in brackets 5 is the size of the specified array. Similarly, the size of the cities array is3. Array elements are added to the list within braces {} separated by commas.

Array type variables can be declared with var without brackets [].

var evenNums = new int[]{ 2, 4, 6, 8, 10}; 
var cities = new string[]{ "Mumbai", "London", "New York" };

If elements are added at the time of declaration, the size is optional. The compiler will infer the size based on the number of elements within the braces, as shown below.

int[] evenNums = { 2, 4, 6, 8, 10}; 
string[] cities = { "Mumbai", "London", "New York" }

The following example demonstrates invalid array declarations.

//The size must be specified 
int[] evenNums = new int[]; 
//The number of elements must equal the specified size 
int[] evenNums = new int[5] { 2, 4 };
//var cannot be used with array initialization expressions
var evenNums = { 2, 4, 6, 8, 10};

Delayed initialization

It is not necessary to declare and initialize an array in a single statement. You can first declare an array and then initialize it using the new operator.

int[] evenNums;
evenNums = new int[5];
// or
evenNums = new int[]{ 2, 4, 6, 8, 10 };

Accessing array elements

Array elements can be accessed using index. An index is a number associated with each array element, starting from index 0 to the size of the array-1 End.

The following example uses index to add/Updating and retrieving array elements.

int[] evenNums = new int[5];
evenNums[0] = 2;
evenNums[1] = 4;
//evenNums[6] = 12;  //Throw a runtime exception IndexOutOfRange
Console.WriteLine(evenNums[0]);  //Output 2
Console.WriteLine(evenNums[1});  //Output 4

Note that trying to add an element beyond its specified size will throw an IndexOutOfRangeException exception.

Use for loop to access array

Use the for loop to access array elements. Use the array's length property in the condition expression of the for loop.

int[] evenNums = { 2, 4, 6, 8, 10 };
for(int i = 0; i < evenNums.Length; i++}
    Console.WriteLine(evenNums[i]);  
}
for(int i = 0; i < evenNums.Length; i++}
    evenNums[i] = evenNums[i] + 10;  // Update the value of each element10
}

Use foreach loop to access array

Use the foreach loop to read the value of array elements without using an index.

int[] evenNums = { 2, 4, 6, 8, 10}; 
string[] cities = { "Mumbai", "London", "New York" }; 
foreach(var item in evenNums) {
    Console.WriteLine(item);   
}
foreach(var city in cities) {
    Console.WriteLine(city);
}

LINQ methods

All arrays in C# are derived from the abstract base class System.Array.

The Array class implements the IEnumerable interface, allowing you to use LINQ extension methods such as Max(), Min(), Sum(), reverse(), and so on.

int[] nums = new int[5]{ 10, 15, 16, 8, 6 };
nums.Max(); // Return16
nums.Min(); // Return6
nums.Sum(); // Return55
nums.Average(); // Return55

The System.Array class also includes methods for creating, manipulating, searching, and sorting arrays.

int[] nums = new int[5]{ 10, 15, 16, 8, 6 };
Array.Sort(nums); // Sort the array 
Array.Reverse(nums); // Sort the array in descending order
Array.ForEach(nums, n => Console.WriteLine(n)); // Iterate through an array
Array.BinarySearch(nums, 5);// Binary search

Pass an array as a parameter

You can pass an array as a parameter to a method. Arrays are reference types, so this method can change the value of array elements.

public static void Main() {
    int[] nums = { 1, 2, 3, 4, 5 };
    UpdateArray(nums); 
    foreach(var item in nums)
        Console.WriteLine(item);   
}
                    
public static void UpdateArray(int[] arr)
{
    for(int i = 0; i < arr.Length;++)
        arr[i] = arr[i] + 10;   
}

Next to learn Multi-dimensional Array and Razor Array.