Arrays in C# with Examples

Arrays in C# are used when we need to store multiple values of the same type in a single variable. Now you may have a question, which might be, if a single variable holds multiple values, then how can we access any particular value?
The answer is pretty simple, which is through the indexes. I will explain the access of an array element later on this page. But for now, let's see the general form of creating an array in C#.

type[] arrayName = new type[size];

For example:

string[] cities = new string[6];

The above statement creates an array named "cities," in which we can add six elements or values. For example:

cities = new string[6] {"Dallas", "Houston", "Boston", "San Antonio", "San Diego", "San Francisco"};

However, you can also initialize the required number of values or elements to an array at the time of declaration. For example:

string[] cities = { "Dallas", "Houston", "Boston", "San Antonio", "San Diego", "San Francisco" };

Since indexing in arrays starts with 0, Therefore, cities [0] will refer to "Dallas," cities [1] will refer to "Houston," and so on.

Now let me implement these codes to create a proper example in which we will create an array and then print its value on the output console.

string[] cities = { "Dallas", "Houston", "Boston", "San Antonio", "San Diego", "San Francisco" };

Console.WriteLine("Element at Index no.0 = " + cities[0]);
Console.WriteLine("Element at Index no.1 = " + cities[1]);
Console.WriteLine("Element at Index no.2 = " + cities[2]);
Console.WriteLine("Element at Index no.3 = " + cities[3]);
Console.WriteLine("Element at Index no.4 = " + cities[4]);
Console.WriteLine("Element at Index no.5 = " + cities[5]);

The snapshot given below shows the output produced by the above C# example, demonstrating the arrays.

c sharp arrays example

Find Length of an Array in C#

To find the length of an array, we will do it in two ways, one with the C# predefined property and the other with the foreach loop. Let's start with a predefined property named Length.

string[] cities = { "Dallas", "Houston", "Boston", "San Antonio", "San Diego", "San Francisco" };

Console.WriteLine("Length = " + cities.Length);

The output should exactly be:

Length = 6

Now let me go with the second one to find the length of an array in C#.

string[] cities = { "Dallas", "Houston", "Boston", "San Antonio", "San Diego", "San Francisco" };

int i = 0;
foreach (string x in cities)
    i++;

Console.WriteLine("Length = " + i);

You will get the same output. In this example, I have used the foreach loop, which is used to loop through each element of the specified array. So instead of using idexes to access all array elements, you can approach it with a foreach loop in this way.

string[] cities = { "Dallas", "Houston", "Boston", "San Antonio", "San Diego", "San Francisco" };

foreach (string x in cities)
    Console.WriteLine(x);

The output of the above C# example should exactly be:

Dallas
Houston
Boston
San Antonio
San Diego
San Francisco

You can also change an array anytime in the program in this way.

string[] cities = { "Dallas", "Houston", "Boston", "San Antonio", "San Diego", "San Francisco" };

Console.WriteLine("---Original Array---");
for (int i = 0; i < cities.Length; i++)
    Console.Write(cities[i] + "     ");

Console.WriteLine("\n\nChaging the Elements of the Array...");
cities[2] = "Paris";
cities[4] = "Toronto";

Console.WriteLine("\n---New Array after Change---");
for (int i = 0; i < cities.Length; i++)
    Console.Write(cities[i] + "     ");

The output should exactly be:

---Original Array---
Dallas     Houston     Boston     San Antonio     San Diego     San Francisco

Chaging the Elements of the Array...

---New Array after Change---
Dallas     Houston     Paris     San Antonio     Toronto     San Francisco

C# Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!