Introduction
Arrays are an essential data structure in C# that allow you to store and manipulate collections of elements of the same data type. Arrays provide a convenient way to work with multiple values under a single variable name.
In C#, arrays can be of various types, including one-dimensional, multi-dimensional, and jagged arrays. This documentation will explore each type and provide clear examples to help you understand their usage.
One-Dimensional Arrays
Declaration and Initialization
One-dimensional arrays in C# are declared and initialized using square brackets "[]". Here's an example of declaring and initializing an integer array:
int[] numbers = new int[5]; // Creates an integer array with 5 elements
Accessing Elements
You can access elements in a one-dimensional array using their index, starting from 0. For example:
int thirdNumber = numbers[2]; // Accesses the third element (index 2)
Example: Sum of Elements
Let's calculate the sum of elements in an integer array:
int[] numbers = { 1, 2, 3, 4, 5 };
int sum = 0;
foreach (int num in numbers)
{
sum += num;
}
Console.WriteLine("Sum of elements: " + sum);
Sum of elements: 15
Multi-Dimensional Arrays
Declaration and Initialization
Multi-dimensional arrays have multiple dimensions, typically represented as a matrix. You can declare and initialize them like this:
int[,] matrix = new int[3, 3]; // Creates a 3x3 integer matrix
Accessing Elements
Accessing elements in a multi-dimensional array requires specifying indices for each dimension:
matrix[0, 0] = 1; // Accesses the element in the first row and first column
Example: Matrix Multiplication
Let's perform matrix multiplication using multi-dimensional arrays:
int[,] matrixA = { { 1, 2 }, { 3, 4 } };
int[,] matrixB = { { 5, 6 }, { 7, 8 } };
int[,] result = new int[2, 2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
result[i, j] += matrixA[i, k] * matrixB[k, j];
}
}
}
Console.WriteLine("Matrix Multiplication Result:");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(result[i, j] + " ");
}
Console.WriteLine();
}
Matrix Multiplication Result:
19 22
43 50
Jagged Arrays
Jagged arrays are arrays of arrays, where each sub-array can have a different length. They are declared and initialized as follows:
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2 };
jaggedArray[1] = new int[] { 3, 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9 };
Accessing Elements
To access elements in a jagged array, you need to specify both the outer and inner indices:
int value = jaggedArray[1][2]; // Accesses the third element of the second sub-array
Example: Sum of Elements in Jagged Array
Let's calculate the sum of elements in a jagged integer array:
int[][] jaggedArray = new int[][] {
new int[] { 1, 2 },
new int[] { 3, 4, 5 },
new int[] { 6, 7, 8, 9 }
};
int sum = 0;
foreach (int[] subArray in jaggedArray)
{
foreach (int num in subArray)
{
sum += num;
}
}
Console.WriteLine("Sum of elements in jagged array: " + sum);
Sum of elements in jagged array: 45
Conclusion
Arrays are versatile and fundamental data structures in C#. Understanding how to declare, initialize, and manipulate different types of arrays will greatly enhance your programming skills. Feel free to experiment with the provided examples and explore more complex scenarios to become proficient in working with arrays in C#.
I have a lot of interesting code on GitHub
Top comments (3)
I've got a Unity project I am going to try some of these techniques out in, thanks OP!
You are welcome!
Turns out Unity is on fire currently and I'm not sure if I am going to continue using it as my game engine or not