Arrays
Arrays
Suppose we need to record the age of 5 students. Instead of creating 5 separate variables, we can
simply create an array:
15 19 17 18 14
Elements of an Array
datatype[] arrayName;
dataType - data type like int, string, char, etc
arrayName - it is an identifier
int[] age;
Here, we have created an array named age. It can store elements of int type.
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;
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,
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
int[] age = new int[5];
//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).
3. Access Array Elements
We can access the elements in the array using the index of the array. For example,
Example: C# Array
using System;
namespace AccessArray {
class Program {
static void Main(string[] args) {
// create an array
int[] numbers = {1, 2, 3};
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 {
static void Main(string[] args) {
// create an array
int[] numbers = {1, 2, 3};
Console.ReadLine();
}
}
}
Output
namespace AccessArrayFor {
class Program {
static void Main(string[] args) {
int[] numbers = { 1, 2, 3};
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
Here, the Length property of the array gives the size of the array.
We can also use a foreach loop to iterate through the elements of an array. For example,
namespace AccessArrayForeach {
class Program {
static void Main(string[] args) {
int[] numbers = {1, 2, 3};
Console.ReadLine();
}
}
}
Output
Array Elements:
1
2
3
In C#, we have the System.Linq namespace that provides different methods to perform various
operations in an array. For example,
Example: Find Minimum and Maximum Element
using System;
namespace ArrayMinMax {
class Program {
static void Main(string[] args) {
Console.ReadLine();
}
}
}
Output
Smallest Element: 1
Largest Element: 98
In the above example,
using System;
// provides us various methods to use in an array
using System.Linq;
namespace ArrayFunction {
class Program {
static void Main(string[] args) {
Console.ReadLine();
}
}
}
Output
Average : 59.2
Average using Average() : 59.2
In the above example, we have used
Note: It is compulsory to use the System.Linq namespace while using Min(), Max(), Sum(), Count(),
and Average() methods.