C# Array

Until now, if we want to store multiple values in a variable, it is not possible to store all of them in a single variable. But instead of declaring so many individual variables, we can declare one C# array variable which is capable of storing multiple values of a single data type.

The C# array is a user-defined datatype of reference type. C# array is useful to store multiple values of the same datatype sequentially in a single variable.

The following are the list of essential things one should remember before working with C# arrays.

  • The size of an array, once defined, it cannot be changed, and hence it is said to be of fixed size.
  • Once it is defined, the values in it are stored sequentially in the memory location.
  • Every element of an array is initialized automatically with a default value of that datatype.

For example,

  • If it is an integer array in C#, all the elements in it initialized to 0.
  • If it is a Boolean type, all the elements initialize to false.
  • If it is character type, all the elements initialized to the null character.
  • If it is a string type, all the elements initialized to null.

Since C# arrays are of reference type, they stored in the heap memory.

C# Arrays 2

Arrays in C# generally classified into three types.

  1. Single Dimensional
  2. Multi Dimensional
  3. Jagged

Single Dimensional Array in C#

The single dimensional or one dimensional array in C# is a linear which can store the data in a single row.

C# Array Declaration

Let us see the syntax of declaration:

datatype[] ArrayName = new datatype[size];

Here new is an operator that will create the array and initialize all its elements with their default. Next, size is the size of it; once defined, the value fixed.

For instance

int[] Numbers = new int[5]; where 5 is the length of the Numbers[]

In this case, the elements initialized with default value 0. And 5 is the size of it, that is fixed once defined.

If we want to access or retrieve individual items or value in it, we have to use the index of that item. The index of a C# array starts from 0 to n-1, where n is the length of it.

Here the Numbers[5] contains the elements from Numbers[0] to Numbers[4].

C# initialize Array

Let us see the C# syntax of storing values in an array:

ArrayName[index] = value;

For ex:

Numbers[0] = 10;

Numbers[1] = 30;

Numbers[2] = 80;

Numbers[3] = 100;

Numbers[4] = 70;

Here, the default value 0 is overwritten by the values with which we have initialized the elements.

Inline Initialization of an Array in C#

Case 1: If we have fixed values to store in it, then we can declare an array as follows:

For example

Int[] array1 = new int[4] {3, 6, 8, 2};

Here the size mentioned is 4. If we pass the number of elements less than or greater than the length or size of an array, then it will give you a syntax error.

Hence we need to pass the same number of elements mentioned in the size.

Case 2: Without specifying the number of elements or size of it, we can even declare an array using inline initialization.

For example,

Int[] array1 = new int[] {10, 2, 6, 4};

Here we have not specified any size, so we can pass as many elements as we want. However, once we passed, that will be the size of that one and later cannot be changed.

C# Array Example

The following example helps you understand the declaration, initialization, and printing the elements of a one dimensional array in C#.

using System;
 class program
     public static void Main()
         int[] array1 = new int[5];
         array1[0] = 10;
         array1[1] = 20;
         array1[2] = 40;
         array1[3] = 60;
         char[] array2 = new char[4] {'a', 'n', 'k', 'o'};
         float[] array3 = new float[] { 2.13F, 5, 9.198F, 10.0F, 12};
         Console.WriteLine("Second element in array1 is {0}", array1[1]);
         Console.WriteLine("Fifth element in array1 is {0}", array1[4]);
         Console.WriteLine("First element in array2 is {0}", array2[0]);
         Console.WriteLine("Third element in array3 is {0}", array3[2]);


C# Array Example 1


array1[] is an integer type whose size is 5

Here, only 4 elements got initialized. 5th element not initialized.

When we try to print second and fifth elements to C# console,

As usual, the second element is array1[1] =20

And fifth element array1[4]=0

array2[] is of type char which is having four elements.

array2[0] is the first element in array2, i.e., a

array3[] is of type float, which is having five elements.

array3[2] is the fifth element in array3, i.e., 9.198