An array is a collection of similar types of data. For example:
Suppose we need to record the age of 5 students. Instead of creating 5 separate variables, we can simply create an array:
Array Declaration
datatype[] arrayName;
Here,
dataType - data type like int, string, char, etc.
arrayName - it is an identifier
Let's see an example,
// declare an arrayint[] age;// allocate memory for arrayage =newint[5];
Here, we have created an array named age. It can store 5 elements of int type.
Array initialization
In C#, we can initialize an array during the declaration. For example:
int [] numbers = {1,2,3,4,5};
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!
Array indexing
We can use the index number to initialize an array in C#. For example:
// declare an arrayint[] age =newint[5];//initializing arrayage[0] =12;age[1] =4;age[2] =5;...
As you can see from the above diagram, An array index always starts at 0.
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
Access Array Elements
We can access the elements in the array using the index of the array. For example:
// access element at index 2array[2];// access element at index 4array[4];
Exercise
Complete the code to get the expected output
Expected Output:
Element in first index :1Element in second index :2Element in third index :3
usingSystem;classProgram {staticvoidMain() { // todo - create an array called numbers with elements 1, 2 and 3 in it //access first elementConsole.WriteLine("Element in first index : "+/*todo*/); //access second elementConsole.WriteLine("Element in second index : "+/*todo*/); //access third elementConsole.WriteLine("Element in third index : "+/*todo*/);Console.ReadLine(); }}
Iterating array using for loop
In C#, we can use loops to iterate through each element of an array. For example:
classProgram {staticvoidMain(string[] args) {int[] numbers = { 1,2,3};for(int i=0; i <numbers.Length; i++) {Console.WriteLine("Element in index "+ i +": "+numbers[i]); }Console.ReadLine(); } }
Output:
Element in index 0:1Element in index 1:2Element in index 2:3
Here, the Length property of the array gives the size of the array.
Iterating array using foreach loop
We can also use a foreach loop to iterate through the elements of an array. For example:
classProgram {staticvoidMain(string[] args) {int[] numbers = {1,2,3};Console.WriteLine("Array Elements: ");foreach(int num in numbers) {Console.WriteLine(num); }Console.ReadLine(); } }
Output
Array Elements:123
Exercise
Find the sum and average of 5 numbers given by user:
Expected output:
Enter Number 11Enter Number 22Enter Number 33Enter Number 44Enter Number 55Given numbers are:12345Sum is15andAvg is 3
usingSystem;classProgram {staticvoidMain(string[] args) {int[] nums =/*todo*/for(/*todo*/){Console.WriteLine("Enter Number {0}", i+1);/*todo*/ } int sum =0;foreach(/*todo*/) sum+=num;int avg =/*todo*/Console.WriteLine("Given numbers are:");foreach(/*todo*/) Console.Write($"{num} ");Console.WriteLine();Console.WriteLine($"Sum is {/*todo*/} and Avg is {/*todo*/}"); }}
Exercise
Find the largest word among the words entered by user
Expected Output
How many words do you want to input?3Enter word no 1computerEnter word no 2isEnter word no 3funAmong the given words, largest word iscomputer
usingSystem;classProgram {staticvoidMain() {Console.WriteLine("How many words do you want to input?");int size =Convert.ToInt32(Console.ReadLine());/*todo - declare words array*/for(/*todo*/){Console.WriteLine($"Enter word no {i+1}");/*todo - store the words*/ }int large =0;string largestWord =string.Empty;foreach(/*todo*/){if(word.Length> large){ large =word.Length; largestWord = word; } }Console.WriteLine($"Among the given words, largest word is {/*todo*/}"); }}