Arrays in C#
An array is a collection of elements of the same data type stored under a single variable name. Arrays are useful when we need to store multiple values of the same type.
Why Do We Use Arrays?
Suppose we want to store marks of five students. Without an array, we would need five separate variables.
int mark1 = 80;
int mark2 = 75;
int mark3 = 90;
int mark4 = 85;
int mark5 = 70;
Using an array, we can store all these values in a single variable.
int[] marks = { 80, 75, 90, 85, 70 };
Array Syntax
dataType[] arrayName = new dataType[size];
Example:
int[] numbers = new int[5];
Declaring and Initializing an Array
An array can be declared and initialized at the same time.
using System;
int[] numbers = { 10, 20, 30, 40, 50 };
Console.WriteLine(numbers[0]);
Console.WriteLine(numbers[1]);
Console.WriteLine(numbers[2]);
Output
10
20
30
Accessing Array Elements
Each element in an array can be accessed using its index.
using System;
string[] names = { "Rahul", "Amit", "Sneha", "Priya" };
Console.WriteLine(names[0]);
Console.WriteLine(names[2]);
Output
Rahul
Sneha
Changing Array Values
Array elements can be changed by assigning a new value to a specific index.
using System;
int[] numbers = { 10, 20, 30 };
numbers[1] = 100;
Console.WriteLine(numbers[1]);
Output
100
Array Length
The Length property returns the total number of elements present in an array.
using System;
int[] numbers = { 10, 20, 30, 40, 50 };
Console.WriteLine(numbers.Length);
Output
5
Using for Loop with Array
A for loop can be used to access every element of an array using its index.
using System;
int[] numbers = { 10, 20, 30, 40, 50 };
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
Output
10
20
30
40
50
Using foreach with Array
The foreach loop is commonly used when we want to read every element of an array.
using System;
int[] numbers = { 10, 20, 30, 40, 50 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Output
10
20
30
40
50
Sorting an Array
The Array.Sort() method sorts array elements in ascending order.
using System;
int[] numbers = { 50, 20, 40, 10, 30 };
Array.Sort(numbers);
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Output
10
20
30
40
50
Searching in an Array
The Array.IndexOf() method can be used to find the index of a particular element.
using System;
int[] numbers = { 10, 20, 30, 40, 50 };
int index = Array.IndexOf(numbers, 30);
Console.WriteLine(index);
Output
2
Multidimensional Array
A multidimensional array stores data in more than one dimension. A two-dimensional array is commonly used to represent rows and columns.
using System;
int[,] matrix =
{
{ 10, 20 },
{ 30, 40 }
};
Console.WriteLine(matrix[0, 0]);
Console.WriteLine(matrix[1, 1]);
Output
10
40
Jagged Array
A jagged array is an array of arrays. Each inner array can contain a different number of elements.
using System;
int[][] numbers = new int[2][];
numbers[0] = new int[] { 10, 20 };
numbers[1] = new int[] { 30, 40, 50 };
Console.WriteLine(numbers[0][1]);
Console.WriteLine(numbers[1][2]);
Output
20
50
Common Array Mistakes
- Array indexing starts from 0.
- Do not access an index outside the array range.
- All elements of a normal array must have the same data type.
- Use Length to determine the number of elements.
- Remember that a normal array has a fixed size.
Real-World Example
Consider a student management system where we need to store marks of multiple subjects.
using System;
int[] marks = { 85, 78, 92, 88, 75 };
int total = 0;
foreach (int mark in marks)
{
total = total + mark;
}
Console.WriteLine("Total Marks: " + total);
Output
Total Marks: 418
Interview Questions
1. What is an array in C#?
An array is a collection of elements of the same data type stored under a single variable name.
2. From which index does an array start?
Array indexing starts from 0.
3. What is the difference between an array and a List?
An array has a fixed size, while a List can dynamically grow or shrink.
4. What is Array.Length?
Array.Length returns the total number of elements in an array.
5. What is a multidimensional array?
A multidimensional array stores data in multiple dimensions, such as rows and columns.
Arrays Summary
- An array stores multiple values of the same data type.
- Array indexing starts from 0.
- The Length property returns the number of elements.
- Arrays can be processed using for and foreach loops.
- Array.Sort() can be used for sorting.
- Array.IndexOf() can be used for searching.
- C# supports multidimensional and jagged arrays.
- Arrays have a fixed size.
Practice Questions
- Create an integer array and print all elements.
- Find the largest number from an array.
- Find the smallest number from an array.
- Calculate the sum of all array elements.
- Calculate the average of array elements.
- Reverse an array.
- Sort an array in ascending order.
- Search for an element in an array.
- Create a multidimensional array and display its values.
- Create a jagged array and display its elements.