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

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More