C# Arrays

In the previous section, you have learned about C# variables and about the uses of them. As we have learned so far that a variable can hold only one value at a time. There are situations when we have required to hold hundreds of values. Like, suppose you want to hold a table which contains the values from 1 to 100.

So what you will do in that situation? Of course creating hundreds of variables to store the table is not a good idea, and it's a bad programming approach.

To overcome from such kind of situations C# introduced a concept called Array. This is special kind of datatype which facilitate us to store a fixed number of values using the index.

Types of Array

In C# there are three types are arrays.

  • Single Dimensional Array
  • Multidimensional Array
  • Jagged Array

In this section, we will learn about the single dimensional array. Let's understand about the C# single dimensional array with an image.

Array Declaration & Initialization

Declaring an array in C# is very straightforward and simple. A C# array can be declared by placing two brackets [] after the type name followed by the variable name. Following is the syntax to declare an array in C#.

datatype[] variableName;

Following are the different types of C# Array declarations.

int[] Numbers; //An array of integers
string[] WeekDays; //An array of strings
float[] Scales; //An array of float values
bool[] Genders; //An array of boolean values

In the above example, there are four different types of array declarations. These arrays don't have a size and they not initialized yet.

In C# while declaring an array we can initialize it in three different ways.

By providing the size of array using new keyword

int[] Numbers = new int[5]; //array size is 5
string[] WeekDays = new string[10]; //array size 10

By doing late initialization. Meaning, by adding values to the array after defining its size.

//When array size is defined you must provide the same number of values. 
int[] Numbers = new int[5] { 1, 7, 8, 5, 8 };
string[] WeekDays = new string[7] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
 
//Even Array size is not required
int[] Numbers = new int[] { 1, 7,  8 };
string[] WeekDays = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday" };

By omitting the new keyword. The number of elements added while initialization indicates the size of the array.

int[] Numbers = { 1, 7, 8, 5, 8 }; //Omitted the new keyword
string[] WeekDays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; //Omitted the new keyword

In C# before using an array we must provide the size of it. Following is an invalid array initialization. However, in some cases like late initialization, the array size is not required.

//Compile time error - Wrong number of indices inside[]; expected 1
int[] Numbers = new int[];
string[] WeekDays = new string[];

Accessing Array Elements

To access the elements of an array the array index is used. We can assign values to the array elements by using their index in the following way.

Example

Try It
public static void Main()
{
    int[] Numbers = new int[5];
    string[] WeekDays = new string[7];
 
    for (int i = 0; i < 5; i++)
    {
        Numbers[i] = i * 10;
    }
    for (int i = 0; i < 7; i++)
    {
        WeekDays[i] = Enum.GetName(typeof(DayOfWeek), i);
    }
 
    //Fetching the values of arrays using particular index
    Console.WriteLine("Value at position 1 : " + Numbers[1]);
    Console.WriteLine("Value at position 4 : " +WeekDays[4]);
}
Output

Value at position 1 : 10
Value at position 4 : Thursday

Traversing array

Like we have assigned the values in the above example, we can access the values in the same way by using the array Index. In the following example, a for loop is being used to traverse through the array elements.

Example

Try It
public static void Main()
{
    int[] Numbers = new int[5];
    string[] WeekDays = new string[7];
 
    for (int i = 0; i < 5; i++)
    {
        Numbers[i] = i * 10;
    }
    for (int i = 0; i < 7; i++)
    {
        WeekDays[i] = Enum.GetName(typeof(DayOfWeek), i);
    }
 
    //Fetching the values of arrays using particular index
    for (int i = 0; i < Numbers.Length; i++)
    {
        Console.WriteLine("Value at position {0} : {1} ", i,  Numbers[i]);
    }
    Console.WriteLine();
    for (int i = 0; i < WeekDays.Length; i++)
    {
	Console.WriteLine("Value at position {0} : {1} ", i, WeekDays[i]);
    }
}
Output

Value at position 0 : 0
Value at position 1 : 10
Value at position 2 : 20
Value at position 3 : 30
Value at position 4 : 40

Value at position 0 : Sunday
Value at position 1 : Monday
Value at position 2 : Tuesday
Value at position 3 : Wednesday
Value at position 4 : Thursday
Value at position 5 : Friday
Value at position 6 : Saturday

This was the overview and explanation of C# single dimensional array. Let's learn about the C# multidimensional array in the next section.