C# Arrays4 min read

You must have learned C, C++, Java, etc and like any other programming language, an array in C# is also a group or a fixed-size sequential collection of data having the same data-type stored in a contiguous memory location.

The are used to store multiple values in a single variable instead of creating multiple variable for each value. It is a simple data structure format where the primitive type of data such as int, char, double, float, etc are stored and accessed randomly through their index number such as array[0], array[1], etc.

Elements are arranged inside the array with index numbers starting from zero as the first elements.

There are 3 types of arrays in C#:

  1. Single Dimensional Array
  2. Multidimensional Array
  3. Jagged Array

Arrays are objects in C# whose allocation of memory is done dynamically in a program. Since they are objects, we can use predefined functions in C# to find the size of an array.


Declaration of array in C#

For declaration of an array in C#, the user needs to specify the type of element and number of elements in an array. Below is the declaration of the single-dimensional Array.

The data type must be a valid C# data type, a unique name must be specified to each array and the arraySize must be of an integer constant. Let see an example for an integer array with 20 elements.


Initialization of array in C#

Declaration doe not mean that the array is initialized in a memory. Initialization of an array means assigning the value to an array.

An array is a reference type that is it deals with the address of the elements. And since it is a reference type, we need to use new keyword in order to initialize the array variable.

Syntax and Example:

This way an instance of an array is created with a new keyword.

You can initialize an array in various ways as follows:

1. Create and initialize an array.

2. Omit the size of array.

3. Initialize an array at the time of declaration.

Initialize the array after multiple array declaration. Example:


Accessing Array Elements in C#

As told above, that each array elements are associated with specific number that is the index numbers.

An array element can be accessed by using the specific index number. It can be achieved by placing the particular index number within the bracket []. Such as:

You can also assign the value by accessing each of the position through index number. Example:


C# program for initialization, declaration and accessing an array

In the program below we will initialize an array by the user input that is we will take an element of array from the user input.

Output:


Example: C# array initializing an array by accessing an array with an index number.

Output:


Using the foreach Loop

We used for loop above to display an element of an array one by one. Now we will do the same with foreach loop in C#. Traversal using foreach loop

Output:


C# Arrays in Detail

here is more to the arrays in C#. The above understanding mostly for 2D arrays. Click on the topic below to learn in detail about arrays in C#.

C# Multidimensional Arrays
C# Jagged Arrays
C# Passing Array to Function
C# Params Array
C# Array Class

MORE

C Program to Convert Decimal to Binary

In this tutorial, we will write a C program to convert decimal to binary. Before that, you must have knowledge of the following topics in …
Read More

C Program to Convert Binary to Decimal

In this tutorial, we will write a program to convert binary to decimal in C. Before that, you must have knowledge of the following topics …
Read More

C Program to Print ASCII Value of a Character

In this tutorial, we will write a program to print ASCII value of a character in C programming. Before that, you should have knowledge on …
Read More

Java Program to Convert Binary to Hexadecimal

In this tutorial, we will write a Java Program to Convert a Binary Number to Hexadecimal Number. Before that, you must have knowledge of the …
Read More

Java Program to Convert Hexadecimal to Binary

In this tutorial, we will write a Java Program to Convert a Hexadecimal Number to Binary Number. Before that, you must have knowledge of the …
Read More

Java Program to Convert Hexadecimal to Decimal

This is a Java Program to Convert a Hexadecimal Number to its decimal Equivalent. Before that, you must have knowledge of the following topics in …
Read More