Array

Array is a group of same data type elements, which are stored in contiguous memory. In C# array is an object of base type System.Array. In C# array index start with 0. We can also say that array is collection of the variables. Lets say if we want to declare 10 integer variables then we can declare 1 integer array with 10 elements.

Adventages of Array

We can easily access any element of an array We can easily traverse through data Easy to modify data We can sort data in any order Code optimization (Required less code)

Disadventage of Array

It is fixed size, so dynamically we can't increase/decrease the size of an array in C#

Declaring an array

Lets start by declaring array first. For example
Try it
int[] numbers; 	// It will create blank integer array
int[] numbers = {10, 20, 30};  // It will create an array with 3 values (elements)

Accessing Array Elements

We can access array element by index number, For example

Try it
int[] numbers = { 10, 20, 30 };
Console.WriteLine("Second element is : "+ numbers[1]);
// Output is "Second element is : 20"

We have passes 1 index of array so it will get the second element from the array.

Change an Array Element

If we want to change value of any element then we can assign new value to specific element of an array by index, In below example third element's value is 30 and we are changing to 40 so it will print 40 as output. For example

Try it
int[] numbers = { 10, 20, 30 };
numbers[2] = 40;
Console.WriteLine("Third element is : "+ numbers[2]);
// Output is "Third element is : 40"

Array Length

Array has many properties, One of them is "Length" property which will be used to identify the number of elements inside the array. For example

Try it
int[] numbers = { 10, 20, 30 };
Console.WriteLine("The length of given array is : "+ numbers.length);
// Output is "The length of given array is : 3"

Loop Through an Array

You can loop through all the elements in array with "for" loop and "Length" property. "Length" property will identify how many number of times loop should run. For example

Try it
int[] numbers = { 10, 20, 30 };
for (int loop = 0; loop < numbers.Length; loop++)
{
    Console.WriteLine("The value at index " + loop + " is " + numbers[loop]);
}
// Output 
// The value at index 0 is 10
// The value at index 1 is 20
// The value at index 2 is 30

The foreach Loop

There is another loop called "foreach" loop, using "foreach" loop we don't need to use "Length" property, For example

Try it
int[] numbers = { 10, 20, 30 };
foreach (int number in numbers)
{
    Console.WriteLine("The value is " + number);
}
// Output 
// The value is 10
// The value is 20
// The value is 30

Sort Arrays

There are many methods which array supports. The one very useful method is "Sort", It will sort the array either alphabetically or in an ascending order. For example

Try it
int[] numbers = { 58, 20, 45, 36, 24 };
Array.Sort(numbers);
foreach (int number in numbers)
{
    Console.WriteLine("The value is " + number);
}
// Output 
// The value is 20
// The value is 24
// The value is 36
// The value is 45
// The value is 58

 

← Previous