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. An array is useful to store multiple values of the same datatype sequentially in a single variable.

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

  • Once we define it, the size of an array cannot change, and hence it is said to be of fixed size.
  • Once we define it, its values will store sequentially in the memory location.
  • Every element of the C# array will initialize automatically with a default value of that data type.

For example,

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

Since C# arrays are of reference type, so they will store in the heap memory.

Pictorial Representation of C# Array Items 2

C# Arrays are generally classified into three types.

  1. Single Dimensional.
  2. Multi Dimensional.
  3. Jagged.

C# One Dimensional Array

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

C# Array Declaration

Let us see the syntax of the declaration:

datatype[] ArrayName = new datatype[size];

Here new is an operator that will create the C# array and initialize all its elements with their default. Next, size is the size of it; once we define it, the value will fix.

For instance

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

In this case, the elements will initialize with the default value of 0. And 5 is its size, which is fixed once we define it.

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

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

Initialize Array in C#

Let us see the C# syntax for 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 size.

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

For example,

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

We have not specified any size here, so we can pass as many elements as we want. However, once we pass, 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 of the elements of a one-dimensional array.

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]);
         Console.ReadLine();
     }
 }

OUTPUT

C# Array Example 1

ANALYSIS

array1[] is an integer type whose size is 5

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

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

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

And fifth element array1[4]=0

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

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

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

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

Categories C#