C# Array
C# Array
In this tutorial, we will learn about C# arrays. We will learn to create, initialize, and access array with the help of examples.
Suppose we need to record the age of 5 students. Instead of creating 5 separate variables, we can simply create an array:
Elements of an Array
1. C# Array Declaration
In C#, here is how we can declare an array.
datatype[] arrayName;
Here,
arrayName - it is an identifier
int[] age;
To define the number of elements that an array can hold, we have to allocate memory for the array in C#. For example,
// declare an array
int[] age;
Here, new int[5] represents that the array can store 5 elements. We can also say the size/length of the array is 5.
Note: We can also declare and allocate the memory of an array in a single line. For example,
2. Array initialization in C#
In C#, we can initialize an array during the declaration. For example,
Here, we have created an array named numbers and initialized it with values 1, 2, 3, 4, and 5 inside the curly braces.
Note that we have not provided the size of the array. In this case, the C# automatically specifies the size by counting the number of elements in
the array (i.e. 5).
In an array, we use an index number to determine the position of each array element. We can use the index number to initialize an array in C#.
For example,
// declare an array
//initializing array
age[0] = 12;
age[1] = 4;
age[2] = 5;
...
C# Array Initialization
Note:
An array index always starts at 0. That is, the first element of an array is at index 0.
If the size of an array is 5, the index of the last element will be at 4 (5 - 1).
array[2];
array[4];
Here,
Example: C# Array
using System;
namespace AccessArray {
class Program {
// create an array
Console.ReadLine();
Output
In the above example, we have created an array named numbers with elements 1, 2, 3. Here, we are using the index number to access elements
of the array.
using System;
namespace ChangeArray {
class Program {
// create an array
numbers[0] = 11;
Console.ReadLine();
Output
In the above example, the initial value at index 0 is 1. Notice the line,
numbers[0] = 11;
Here, we are assigning a new value of 11 to the index 0. Now, the value at index 0 is changed from 1 to 11.
using System;
namespace AccessArrayFor {
class Program {
}
Console.ReadLine();
Output
Element in index 0: 1
Element in index 1: 2
Element in index 2: 3
In the above example, we have used a for loop to iterate through the elements of the array, numbers. Notice the line,
numbers.Length
We can also use a foreach loop to iterate through the elements of an array. For example,
using System;
namespace AccessArrayForeach {
class Program {
Console.WriteLine(num);
Console.ReadLine();
Output
Array Elements:
using System;
using System.Linq;
namespace ArrayMinMax {
class Program {
Console.ReadLine();
Output
Smallest Element: 1
Largest Element: 98
using System;
using System.Linq;
namespace ArrayFunction {
class Program {
Console.ReadLine();
Output
Average : 59.2
numbers.Count() to get the total number of element present inside the array
Here, we have also used the numbers.Average() method of the System.Linq namespace to get the average directly.
C# Multidimensional Array
In this tutorial, we will learn about the multidimensional array in C# using the example of two-dimensional array.
Before we learn about the multidimensional arrays, make sure to know about the single-dimensional array in C#.
In a multidimensional array, each element of the array is also an array. For example,
int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
Here, x is a multidimensional array which has two elements: {1, 2, 3} and {3, 4, 5}. And, each element of the array is also an array
with 3 elements.
Two-dimensional array in C#
A two-dimensional array consists of single-dimensional arrays as its elements. It can be represented as a table with a specific number of rows
and columns.
C# Two-dimensional array
Here, x is a two-dimensional array with 2 elements. And, each element is also an array with 3 elements.
int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
Here, x is a 2D array with two elements {1, 2, 3} and {3, 4, 5}. We can see that each element of the array is also an array.
We can also specify the number of rows and columns during the initialization. For example,
// a 2D array
int[ , ] x = { { 1, 2 ,3}, { 3, 4, 5 } };
Example: C# 2D Array
using System;
namespace MultiDArray {
class Program {
//initializing 2D array
Output
In the above example, we have created a 2D array named numbers with rows {2, 3} and {4, 5}.
Here, we are using the index numbers to access elements of the 2D array.
numbers[0, 0] - access the first element from the first row (2)
numbers[1, 0] - access the first element from the second row (4)
using System;
namespace MultiDArray {
class Program {
// old element
numbers[0, 0] = 222;
// new element
Output
In the above example, the initial value at index [0, 0] is 2. Notice the line,
numbers[0, 0] = 222;
Here, we are assigning a new value 222 at index [0, 0]. Now, the value at index [0, 0] is changed from 2 to 222.
using System;
namespace MultiDArray {
class Program {
Console.WriteLine();
}
}
Output
Row 0: 2 3 9
Row 1: 4 5 9
In the above example, we have used a nested for loop to iterate through the elements of a 2D array. Here,
Note: We can also create a 3D array. Technically, a 3D array is an array that has multiple two-dimensional arrays as its elements. For example,
int[ , , ] numbers = { { { 1, 3, 5 }, { 2, 4, 6 } },
{ { 2, 4, 9 }, { 5, 7, 11 } } };