C# Array

Array declaration & dimension in C#.

* In computer science, arrays are crucial for many algorithms and data manipulation tasks, for that reason understanding how to declare them and work with their dimensions is fundamental for any programmer.

Array

Arrays are used to store collections of elements of the same type and they provide a way to group multiple variables of the same type into a single collection. Arrays are declared using square brackets ('[]'). Here's a deep dive into array declaration and dimensions in C#:

Declaration:

You can declare an array in C# using the following syntax:

dataType[] arrayName;

Here, dataType specifies the type of elements the array will hold, and arrayName is the name of the array variable. For example:

int[] numbers; // declares an array of integers
string[] names; // declares an array of strings

Initialization:

After declaring an array, you need to initialize it before using it. There are several ways to initialize an array in C#:

1. Syntax:

dataType[] arrayName = new dataType[] { value1, value2, ..., valueN };

2. Initialization:

int[] numbers = new int[] { 1, 2, 3, 4, 5 };
string[] names = new string[] { "Ayan", "Suman", "Sanvi" };

Dimensions:

C# supports multidimensional arrays, allowing you to create arrays with multiple dimensions (e.g., 2D, 3D, etc.). You can declare and initialize multidimensional arrays using the following syntax:

dataType[,] arrayName = new dataType[rowSize, columnSize];

In Programming:

int[,] matrix = new int[3, 3]; // 3x3 2D array

You can access elements of a multidimensional array using indices too. For example:

matrix[0, 0] = 1;
matrix[0, 1] = 2;
// and so on...

Learn how matrix algorithms work with programming examples.

Jagged:

Jagged arrays are arrays of arrays. Each element of a jagged array can be an array itself. Jagged arrays can have varying lengths for each row. You can declare and initialize jagged arrays as follows:

dataType[][] jaggedArrayName = new dataType[rowSize][];

In Programming:

int[][] jaggedMatrix = new int[3][];
jaggedMatrix[0] = new int[] { 1, 2 };
jaggedMatrix[1] = new int[] { 3, 4, 5 };
jaggedMatrix[2] = new int[] { 6, 7, 8, 9 };

Jagged arrays are useful when you need to represent irregular data structures.

Here's a simple C# program that demonstrates the declaration, initialization, and usage of arrays:

cs Copy Code
using System;

class Program
{
    static void Main(string[] args)
    {
        // 1. One-dimensional array example
        int[] numbers = { 10, 20, 30, 40, 50 };

        Console.WriteLine("One-dimensional array:");
        foreach (int num in numbers)
        {
            Console.Write(num + " ");
        }
        Console.WriteLine();

        // 2. Multidimensional array example
        int[,] matrix = new int[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

        Console.WriteLine("\nMultidimensional array:");
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine();
        }

        // 3. Jagged array example
        int[][] jaggedMatrix = [[1, 2], [3, 4, 5], [6, 7, 8, 9]];
        Console.WriteLine("\nJagged array:");
        foreach (int[] row in jaggedMatrix)
        {
            foreach (int num in row)
            {
                Console.Write(num + " ");
            }
            Console.WriteLine();
        }
    }
}
Output:
One-dimensional array:
10 20 30 40 50

Multidimensional array:
1 2 3
4 5 6
7 8 9

Jagged array:
1 2
3 4 5
6 7 8 9

Conclusion:

Understanding array in C# is fundamental for handling collections of data efficiently. Whether you're working with one-dimensional arrays, multidimensional arrays, or jagged arrays, mastering these concepts will greatly enhance your ability to work with collections of data.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.