Introduction to C# Arrays
In C#, arrays are an essential data structure used to store collections of elements of the same type. They provide a convenient way to work with multiple values under a single variable name. In this tutorial, we’ll cover everything you need to know about C# arrays, from basic concepts to advanced techniques.
1. Declaring and Initializing Arrays
You can declare and initialize arrays in C# using the following syntax:
// Declaration and initialization of an array of integers
int[] numbers = new int[5]; // Creates an array of 5 integers
// Initializing an array with values
int[] primes = { 2, 3, 5, 7, 11 };
2. Accessing Array Elements
Accessing elements in an array is straightforward using zero-based indexing:
int[] numbers = { 10, 20, 30, 40, 50 };
// Accessing individual elements
int firstElement = numbers[0]; // Retrieves the first element (10)
int thirdElement = numbers[2]; // Retrieves the third element (30)
3. Modifying Array Elements
You can modify elements in an array by assigning new values to specific indexes:
int[] numbers = { 10, 20, 30, 40, 50 };
// Modifying elements
numbers[1] = 25; // Changes the value at index 1 to 25
4. Array Length and Bounds
The Length
property gives you the number of elements in an array, and accessing an index outside the bounds of the array will result in an IndexOutOfRangeException
.
int[] numbers = { 10, 20, 30, 40, 50 };
// Getting the length of the array
int length = numbers.Length; // Returns 5
// Accessing an index outside the bounds of the array (will throw an exception)
// int element = numbers[10]; // This will throw an IndexOutOfRangeException
5. Iterating Through Arrays
You can iterate through arrays using loops such as for
, foreach
, or while
:
int[] numbers = { 1, 2, 3, 4, 5 };
// Using a for loop
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
// Using foreach loop
foreach (int num in numbers)
{
Console.WriteLine(num);
}
6. Multidimensional Arrays
C# supports multidimensional arrays, which are arrays of arrays. You can create 2D, 3D, or even higher-dimensional arrays:
// Declaration and initialization of a 2D array
int[,] matrix = new int[3, 3];
// Initializing a 2D array with values
int[,] identityMatrix = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
7. Jagged Arrays
Jagged arrays are arrays of arrays where each element can be an array of different sizes:
// Declaration and initialization of a jagged array
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[2] { 1, 2 };
jaggedArray[1] = new int[3] { 3, 4, 5 };
jaggedArray[2] = new int[4] { 6, 7, 8, 9 };
Conclusion
In this tutorial, we covered the basics of C# arrays, including declaration, initialization, accessing elements, modifying arrays, iterating through arrays, and working with multidimensional and jagged arrays. Arrays are fundamental data structures in C#, and mastering them is essential for developing efficient and scalable applications. Practice these concepts with various examples to solidify your understanding.