2. Introduction
Array in C# is a group of similar types of elements that have contiguous memory
location.
In C#, array is an object of base type System.Array.
In C#, array index starts from 0. We can store only fixed set of elements in C# array.
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
3. Array(cont..)
Advantages of C# Array
• Code Optimization (less code)
• Random Access
• Easy to traverse data
• Easy to manipulate data
• Easy to sort data etc.
Disadvantages of C# Array
• Fixed size
4. Array(cont..)
Advantages of C# Array
• Code Optimization (less code)
• Random Access
• Easy to traverse data
• Easy to manipulate data
• Easy to sort data etc.
Disadvantages of C# Array
• Fixed size
6. Types of Array
There are 3 types of arrays in C# programming:
1. Single Dimensional Array
2. Multidimensional Array
3. Jagged Arrays
7. C# Single Dimensional Array-Initialization
To create single dimensional array, you need to use square brackets [] after
the type.
int[] arr = new int[5]; //creating array
string[] cars = {"Volvo", "BMW",
"Ford", "Mazda"};
Console.WriteLine(cars[0]);
//Outputs Volvo
8. Example of a Single Dimensional Array
public class single
{
public static void Main(string[] args)
{
int[] arr = new int[5]; //creating array
arr[0] = 10;//initializing array
arr[2] = 20;
arr[4] = 30;
//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console. WriteLine(arr[i]);
}
}
}
Output is:
10
0
20
0
30
9. Multidimensional Arrays
The multidimensional array is also known as
rectangular arrays in C#. It can be two dimensional or
three dimensional. The data is stored in tabular form
(row * column) which is also known as matrix.
To create multidimensional array, we need to use
comma inside the square brackets. For example
1. int[,] arr=new int[3,3];//declaration of 2D array
2. int[,,] arr=new int[3,3,3];//declaration of 3D array
10. Example of Multidimensional
using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr=new int[3,3];//declaration of 2D array
arr[0,1]=10;//initialization
arr[1,2]=20;
arr[2,0]=30;
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine(); //new line at each row
}
} }
Output is:
0 10 0 0 0 20 30 0 0
11. C# Jagged Arrays
In C#, jagged array is also known as "array of arrays" because its elements are arrays. The
element size of jagged array can be different.
int[][] arr = new int[2][];
Initialization:
1.arr[0] = new int[4];
2.arr[1] = new int[6];
12. Example of jagged array
public class JaggedArrayTest
{
public static void Main()
{
int[][] arr = new int[2][];// Declare the array
arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };
// Traverse array elements
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write(arr[i][j]+" ");
}
System.Console.WriteLine();
}
}
}
Output:
11 21 56 78 42 61 37 41 59 63