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


C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …