How to Declare Array in C#
An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations.
Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
In C#, an array can be of three types: single-dimensional, multidimensional, and jagged array. Here you will learn about the single-dimensional array.
Declaring Arrays
To declare an array in C#, you can use the following syntax .
1 2 3 4 5 |
int[] evenNums; // integer array string[] cities; // string array |
Above, evenNums
array can store up to five integers. The number 5 in the square brackets new int[5]
specifies the size of an array. In the same way, the size of cities
array is three. Array elements are added in a comma-separated list inside curly braces { }.
Arrays type variables can be declared using var without square brackets.
1 2 3 4 5 |
var evenNums = new int[]{ 2, 4, 6, 8, 10}; var cities = new string[]{ "Mumbai", "London", "New York" }; |
If you are adding array elements at the time of declaration, then size is optional. The compiler will infer its size based on the number of elements inside curly braces, as shown below.
1 2 3 4 5 |
int[] evenNums = { 2, 4, 6, 8, 10}; string[] cities = { "Mumbai", "London", "New York" } |
Accessing Array Elements
Array elements can be accessed using an index. An index is a number associated with each array element, starting with index 0 and ending with array size – 1.
The following example add/update and retrieve array elements using indexes.
1 2 3 4 5 6 7 8 9 |
int[] evenNums = new int[5]; evenNums[0] = 2; evenNums[1] = 4; //evenNums[6] = 12; //Throws run-time exception IndexOutOfRange Console.WriteLine(evenNums[0]); //prints 2 Console.WriteLine(evenNums[1]); //prints 4 |
Note that trying to add more elements than its specified size will result in IndexOutOfRangeException
.
Accessing Array using for Loop
Use the for
loop to access array elements. Use the length
property of an array in conditional expression of the for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[10]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; } /* output each array element's value */ for (j = 0; j < 10; j++ ) { Console.WriteLine("Element[{0}] = {1}", j, n[j]); } Console.ReadKey(); } } } |
Accessing Array using foreach Loop
Use foreach
loop to read values of an array elements without using index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[10]; /* n is an array of 10 integers */ /* initialize elements of array n */ for ( int i = 0; i < 10; i++ ) { n[i] = i + 100; } /* output each array element's value */ foreach (int j in n ) { int i = j-100; Console.WriteLine("Element[{0}] = {1}", i, j); } Console.ReadKey(); } } } |
LINQ Methods
All the arrays in C# are derived from an abstract base class System.Array.
The Array class implements the IEnumerable
interface, so you can LINQ extension methods such as Max()
, Min()
, Sum()
, reverse()
, etc.
1 2 3 4 5 6 7 8 |
int[] nums = new int[5]{ 10, 15, 16, 8, 6 }; nums.Max(); // returns 16 nums.Min(); // returns 6 nums.Sum(); // returns 55 nums.Average(); // returns 55 |