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.


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


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


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


Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More