Arrays in C#
Learn how to create, initialize, access, and iterate through arrays in C# with beginner-friendly examples.
Table of Contents
- What is an Array?
- Why Use Arrays?
- Declare an Array
- Initialize an Array
- Access Array Elements
- Loop Through an Array
- Multidimensional Arrays
- Useful Array Properties
- Best Practices
- Summary
What is an Array?
An array is a collection of elements of the same data type stored in contiguous memory locations. Arrays allow you to store multiple values in a single variable instead of creating separate variables for each value.
0.
Why Use Arrays?
- Store multiple values efficiently.
- Reduce code duplication.
- Process data using loops.
- Improve code organization.
Declaring an Array
int[] numbers;
This creates an array variable that can store integer values.
Initializing an Array
int[] numbers = {10, 20, 30, 40, 50};
You can also specify the size explicitly.
int[] numbers = new int[5];
Accessing Array Elements
int[] numbers = {10,20,30,40};
Console.WriteLine(numbers[0]);
Console.WriteLine(numbers[2]);
Output
10
30
numbers[5]) throws an IndexOutOfRangeException.
Looping Through an Array
Using for Loop
int[] numbers = {10,20,30,40};
for(int i=0;i<numbers.Length;i++)
{
Console.WriteLine(numbers[i]);
}
Using foreach Loop
foreach(int number in numbers)
{
Console.WriteLine(number);
}
foreach when you only need to read array values.
Multidimensional Array
int[,] matrix =
{
{1,2},
{3,4}
};
Console.WriteLine(matrix[0,1]);
Output
2
Useful Array Properties
| Property / Method | Description |
|---|---|
| Length | Returns the total number of elements. |
| Sort() | Sorts the array. |
| Reverse() | Reverses the array. |
| IndexOf() | Returns the index of an element. |
| Copy() | Copies one array into another. |
Complete Example
using System;
class Program
{
static void Main()
{
string[] fruits =
{
"Apple",
"Mango",
"Orange",
"Banana"
};
foreach(string fruit in fruits)
{
Console.WriteLine(fruit);
}
Console.WriteLine("Total Fruits: " + fruits.Length);
}
}
Best Practices
- Choose meaningful array names.
- Always check array indexes.
- Use
foreachfor read-only iteration. - Use
Lengthinstead of hardcoding the array size. - Use collections like
List<T>when the size changes frequently.
Summary
| Concept | Description |
|---|---|
| Array | Stores multiple values of the same type. |
| Index | Starts from 0. |
| Length | Returns number of elements. |
| for | Access elements using indexes. |
| foreach | Iterate through all elements. |
Interview Questions
- What is an array?
- Why do array indexes start at 0?
- What is the difference between
forandforeach? - What does the
Lengthproperty return? - What exception occurs when accessing an invalid index?
Practice Exercise
- Create an array of 5 student names.
- Print all elements using a
forloop. - Print all elements using a
foreachloop. - Find the total number of elements using
Length. - Create and print a 2×2 multidimensional array.
FAQ
Can an array store different data types?
No. All elements must be of the same data type.
Can the size of an array change?
No. Arrays have a fixed size after creation. Use List<T> if you need a dynamic collection.
Next Tutorial: Methods in C#
No comments:
Post a Comment