DEV Community

Cover image for C# Arrays
Zafar Urakov
Zafar Urakov

Posted on

C# Arrays

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
Sum of elements: 15
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode
Matrix Multiplication Result:
19 22
43 50
Enter fullscreen mode Exit fullscreen mode

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 };
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
Sum of elements in jagged array: 45
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
techsnack profile image
TechSnack - Technology Tutorials

I've got a Unity project I am going to try some of these techniques out in, thanks OP!

Collapse
 
zafarurakov profile image
Zafar Urakov

You are welcome!

Collapse
 
techsnack profile image
TechSnack - Technology Tutorials

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