ARRAYS
• 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.
• A variable is used to store a literal value,
whereas an array is used to store multiple
literal values.
• An array is the data structure that stores a
fixed number of literal values (elements) of
the same data type.
• Array elements are stored contiguously in the
memory.
• 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
C# Array Types
• There are 3 types of arrays in C#
programming:
– Single Dimensional Array
– Multidimensional Array
– Jagged Array
• C# provides three different types of arrays.
These are:
• Single Dimensional Array: A single pair of the
square bracket is used to represent a single
row (hence 1-D) of values under a single
name.
Creating a 1-D Array,
• int[] ar = new int[6];
Multidimensional Array: is also called
rectangular arrays, and they can be 2D, 3D, or
multi-D arrays, and it can be visualized in row-
column format, i.e., matrix format.
Creating a Multi-dimensional array,
int[,] ar = new int[2, 4];
Or
int[,,] ar = new int[2, 3, 4];
Jagged Array: These types of arrays are mainly
called "array of arrays".
The element size differs in the case of jagged
arrays.
Creating a Jagged array,
int[][,] ar = new int[3][,];
2d array
Int[][] a=new int[SIZE][];
C# Single Dimensional Array
• To create single dimensional array, you need
to use square brackets [] after the type.
int[] arr = new int[5];//creating array
You cannot place square brackets after the
identifier.
int arr[] = new int[5];//compile time error
Declaration and Initialization at same time
• There are 3 ways to initialize array at the time
of declaration.
• int[] arr = new int[5]{ 10, 20, 30, 40, 50 };
• We can omit the size of array.
• int[] arr = new int[]{ 10, 20, 30, 40, 50 };
• We can omit the new operator also.
• int[] arr = { 10, 20, 30, 40, 50 };
• Arrays type variables can be declared using var
without square brackets.
Example: Array Declaration using var
var evenNums = new int[]{ 2, 4, 6, 8, 10}; var
cities = new string[]{ "Mumbai", "London",
"New York" };
• If you are adding array elements at the time of
declaration, then size is optional
//must specify the size
int[] evenNums = new int[];
//number of elements must be equal to the
specified size
int[] evenNums = new int[5] { 2, 4 };
//cannot use var with array initializer
var evenNums = { 2, 4, 6, 8, 10};
Accessing Array Elements
Array elements can be accessed using an index.
An index is a number associated with each
array element, starting with index 0 and
ending with array size - 1.
int[] evenNums = new int[5]; evenNums[0] = 2;
evenNums[1] = 4; //evenNums[6] = 12;
//Throws run-time exception
IndexOutOfRange
Console.WriteLine(evenNums[0]); //prints 2
Console.WriteLine(evenNums[1]); //prints 4
Accessing Array using for Loop
Use the for loop to access array elements. Use
the length property of an array in conditional
expression of the for loop.
int[] evenNums = { 2, 4, 6, 8, 10 };
for(int i = 0; i < evenNums.Length; i++)
Console.WriteLine(evenNums[i]);
for(int i = 0; i < evenNums.Length; i++)
evenNums[i] = evenNums[i] + 10;
// update the value of each element by 10
Accessing Array using foreach Loop
Use foreach loop to read values of an array
elements without using index.
int[] evenNums = { 2, 4, 6, 8, 10};
string[] cities = { "Mumbai", "London", "New York"
};
foreach(var item in evenNums)
Console.WriteLine(item);
foreach(var city in cities) Console.WriteLine(city);
• All the arrays in C# are derived from an
abstract base class System.Array.
• The Array class implements
the IEnumerable interface, so you can LINQ
extension methods such
as Max(), Min(), Sum(), reverse(), etc.
int[] nums = new int[5]{ 10, 15, 16, 8, 6 };
nums.Max(); // returns 16
nums.Min(); // returns 6
nums.Sum(); // returns 55
nums.Average(); // returns 55
Array.Sort(nums);
Array.BinarySearch(nums,k);
The System.Array class also includes methods for
creating, manipulating, searching, and sorting arrays.
Example: Array Methods
int[] a = new int[5]{ 10, 15, 16, 8, 6 };
Array.Sort(a); // sorts array Array.Reverse(a); // sorts
array in descending order
Array.ForEach(a, n => Console.WriteLine(n)); // iterates
array
Array.BinarySearch(a, 5);// binary search
• can count the total number of elements or
some specific elements in the array using
Count() method.
string[] animals = { "Cat", "Alligator", "fox",
"donkey", "Cat", "alligator" };
var totalCats = animals.Count(s => s == "Cat");
var x = animals1.Count(s => s.StartsWith("a",
StringComparison.CurrentCultureIgnoreCase));
int[] nums = { 1, 2, 3, 4, 3, 55, 23, 2, 5, 6, 2, 2 };
var t = nums.Count(n => n%2==0);
C# 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:
• int[,] arr=new int[3,3];//
declaration of 2D array
• int[,,] arr=new int[3,3,3];//
declaration of 3D array
• C# supports multidimensional arrays up to 32
dimensions.
• The multidimensional array can be declared by
adding commas in the square brackets.
• For example, [,] declares two-dimensional
array, [, ,] declares three-dimensional array, [, ,
,] declares four-dimensional array, and so on.
So, in a multidimensional array, no of commas
= No of Dimensions + 1.
• int[,] arr2d = new int[3,2]{ {1, 2}, {3, 4}, {5, 6} };
// or int[,] arr2d = { {1, 2}, {3, 4}, {5, 6} };
int[,] arr2d = new int[3,2]{ {1, 2}, {3, 4}, {5, 6} };
arr2d[0, 0]; //returns 1
arr2d[0, 1]; //returns 2
arr2d[1, 0]; //returns 3
arr2d[1, 1]; //returns 4
arr2d[2, 0]; //returns 5
arr2d[2, 1]; //returns 6
int[, ,] arr3d1 = new int[1, 2, 2]{ { { 1, 2}, { 3, 4} } };
int[, ,] arr3d2 = new int[2, 2, 2]{ { {1, 2}, {3, 4} }, { {5, 6},
{7, 8} } };
int[, ,] arr3d3 = new int[2, 2, 3]{ { { 1, 2, 3}, {4, 5, 6} },
{ { 7, 8, 9}, {10, 11, 12} } };
arr3d2[0, 0, 0]; // returns 1
arr3d2[0, 0, 1]; // returns 2
arr3d2[0, 1, 0]; // returns 3
arr3d2[0, 1, 1]; // returns 4
arr3d2[1, 0, 0]; // returns 5
arr3d2[1, 0, 1]; // returns 6
arr3d2[1, 1, 0]; // returns 7
arr3d2[1, 1, 1]; // returns 8
Jagged array
• A jagged array is an array of array. Jagged
arrays store arrays instead of literal values.
• A jagged array is initialized with two square
brackets [][].
– The first bracket specifies the size of an array, and
the second bracket specifies the dimensions of the
array which is going to be stored.
• Example: Jagged Arrays
• int[][] jArray1 = new int[2][]; // can include
two single-dimensional arrays
• int[][,] jArray2 = new int[3][,]; // can include
three two-dimensional arrays
• int[][] X = new int[2][];
• X[0] = new int[3];
• X[0][0]=1;
• X[0][1]=11;
• X[0][2]=31;
• X[1] = new int[4]{4, 5, 6, 7 };
• The following jagged array stores two-dimensional
arrays where the second bracket [,] indicates the
two-dimensional array.
• int[][,] jArray = new int[2][,];
• jArray[0] = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
• jArray[1] = new int[2, 2] { { 7, 8 }, { 9, 10 } };
jArray[0][1, 1]; //returns 4
• jArray[1][1, 0]; //returns 9
• jArray[1][1, 1]; //returns 10
If you add one more bracket then it will be array
of array of array.
int[][][] intJaggedArray = new int[2][][] { new
int[2][] { new int[3] { 1, 2, 3}, new int[2] { 4,
5} }, new int[1][] { new int[3] { 7, 8, 9} } };
Console.WriteLine(intJaggedArray[0][0][0]); //
1 Console.WriteLine(intJaggedArray[0][1]
[1]); // 5 Console.WriteLine(intJaggedArray[1]
[0][2]); // 9