Arrays in C#

Last Updated : 20 Apr, 2026

An array is a linear data structure that stores a fixed-size sequence of elements of the same data type in contiguous memory locations. It allows accessing elements using an index, starting from 0.

  • An array can contain primitive data types as well as objects of a class depending on the definition of an array.
  • Whenever use primitives data types, the actual values have to be stored in contiguous memory locations.
  • In the case of objects, the array stores references to objects that are allocated in the heap memory.

Example: Declaring and initializing an array

C#
int[] numbers = { 10, 20, 30, 40, 50 };

The following figure shows how array stores values sequentially:

download
Array in C#

Syntax

<Data Type>[ ] <Name_Array>

Here,

  • <Data Type> : It defines the element type of the array.
  • [ ] : Represents that the variable is an array type.
  • <Name_Array> : It is the Name of array. 

Note : Only Declaration of an array doesn’t allocate memory to the array. For that array must be initialized.

Array Initialization

Array is a reference type so the new keyword used to create an instance of the array. We can assign initialize individual array elements, with the help of the index. 

Syntax:

type [ ] < Name_Array > = new < datatype > [size];

Here, type specifies the type of data being allocated, size specifies the number of elements in the array and Name_Array is the name of an array variable. And new will allocate memory to an array according to its size. 

Examples: To Show Different ways for the Array Declaration and Initialization  

Syntax

Use Cases

Example

<data_type>[] <arr_name> = new <data_type>[size];

Defining array with size, but not assigns values

int[] arr1 = new int[5];

<data_type>[] <arr_name> = new <data_type>[size]{ array_elements};

Defining array with size and assigning the values at the same time

int[] arr2 = new int[5]{1, 2, 3, 4, 5};

<data_type>[] <arr_name> = { array_elements};

The value of the array is directly initialized without taking its size

int[] intArray3 = {1, 2, 3, 4, 5};

Arrays can be initialized after the declaration. It is not necessary to declare and initialize at the same time using the new keyword.

Example:

// Declaration of the array
string[] str1, str2;

// Initialization of array
str1 = new string[5]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” };
str2 = new string[5]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” };

Note :

Initialization without explicitly specifying size is valid when values are provided directly.

Example: Wrong Declaration for initializing an array

// Compile-time error: must give size of an array
int[] intArray = new int[];

// Error : wrong initialization of an array
string[] str1;
str1 = {“Element 1”, “Element 2”, “Element 3”, “Element 4” };

Accessing Array Elements

We can access an array value through indexing, placed index of the element within square brackets with the array name.

Example: Accessing Array elements using different loops

C#
using System;
	
class Geeks 
{
	// Main Method
	public static void Main()
	{
		// declares an Array of integers.
		int[] intArray;

		// allocating memory for 5 integers.
		intArray = new int[5];

		// initialize the first elements of the array
		intArray[0] = 10;

		// initialize the second elements of the array
		intArray[1] = 20;

		// so on...
		intArray[2] = 30;
		intArray[3] = 40;
		intArray[4] = 50;

		// accessing the elements using for loop
		Console.Write("For loop :");
		for (int i = 0; i < intArray.Length; i++)
			Console.Write(" " + intArray[i]);

		Console.WriteLine("");
		Console.Write("For-each loop :");
		
		// using for-each loop
		foreach(int i in intArray)
			Console.Write(" " + i);

		Console.WriteLine("");
		Console.Write("while loop :");
		
		// using while loop
		int j = 0;
		while (j < intArray.Length) {
			Console.Write(" " + intArray[j]);
			j++;
		}

		Console.WriteLine("");
		Console.Write("Do-while loop :");
		
		// using do-while loop
		int k = 0;
		do
		{
			Console.Write(" " + intArray[k]);
			k++;
		} while (k < intArray.Length);
	}
}

Output
For loop : 10 20 30 40 50
For-each loop : 10 20 30 40 50
while loop : 10 20 30 40 50
Do-while loop : 10 20 30 40 50

Types of Arrays in C#

There are three types of Arrays C# supports as mentioned below:

1. One Dimensional Array

A one-dimensional array stores elements in a single linear sequence. All values of this array are stored contiguously starting from 0 to the array size.

For example, declaring a single-dimensional array of 5 integers :

int[] arrayint = new int[5];

The above array contains the elements from arrayint[0] to arrayint[4]. Here, the new operator has to create the array and also initialize its element by their default values. Above example, all elements are initialized by zero, Because it is the int type. 

Example:

C#
using System;

class Geeks 
{
    public static void Main()
    {
        // declares a 1D Array of string.
        string[] weekDays;

        // allocating memory for days.
        weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

        // Displaying Elements of array
        foreach(string day in weekDays)
            Console.Write(day + " ");
    }
}

Output
Sun Mon Tue Wed Thu Fri Sat 

2. Multidimensional Arrays

The multi-dimensional array contains more than one row to store the values. It is also known as a Rectangular Array in C# because it’s each row length is same. It can be a 2D-array or 3D-array or more. To store and access the values of a multidimensional array, nested loops are required.

Syntax:

// creates a two-dimensional array of four rows and two columns.
int[,] intarray = new int[4, 2];

//creates an array of three dimensions, 4, 2 and 3
int[,,] intarray1 = new int[4, 2, 3];

Example: Demonstration of multi- dimensional array

C#
using System;

class Geeks 
{	
	public static void Main()
	{									
		// The same array with dimensions specified 2, 2 and 3.
		int[,, ] arr = new int[2, 2, 3] { { { 1, 2, 3 }, 
											{ 4, 5, 6 } }, 
											{ { 7, 8, 9 }, 
											{ 10, 11, 12 } } };

      	// Checking elements at particular index
		Console.WriteLine("arr[1][0][1] : " + arr[1, 0, 1]);
							
		Console.WriteLine("arr[1][1][2] : " + arr[1, 1, 2]);
	}
}

Output
arr[1][0][1] : 8
arr[1][1][2] : 12

3. Jagged Arrays

Jagged arrays (array of arrays) allow storing arrays of different sizes.

For detailed explanation, refer to the dedicated Jagged Arrays article: Jagged Arrays in C#

Comment
Article Tags:

Explore