What is Arrays with Example code

Arrays in C# are used to store multiple values of the same type in a single variable.
They provide a convenient way to work with collections of data.





using System;
 
class Program
{
    static void Main(string[] args)
    {
        // Declaration and initialization of an integer array
        int[] numbers = { 10, 20, 30, 40, 50 };
 
        // Accessing elements of the array
        Console.WriteLine("Accessing elements of the array:");
        Console.WriteLine("First element: " + numbers[0]);
        Console.WriteLine("Third element: " + numbers[2]);
        Console.WriteLine("Last element: " + numbers[numbers.Length - 1]); // Length property gives the number of elements in the array
 
        // Modifying elements of the array
        Console.WriteLine("\nModifying elements of the array:");
        numbers[1] = 25; // Modify the second element
        Console.WriteLine("Modified second element: " + numbers[1]);
 
        // Iterating over the array using a foreach loop
        Console.WriteLine("\nIterating over the array:");
        foreach (int num in numbers)
        {
            Console.WriteLine(num);
        }
 
        // Declaration and initialization of a string array with a specified size
        string[] fruits = new string[3];
        fruits[0] = "Apple";
        fruits[1] = "Banana";
        fruits[2] = "Orange";
 
        // Accessing elements of the string array
        Console.WriteLine("\nFruits array:");
        foreach (string fruit in fruits)
        {
            Console.WriteLine(fruit);
        }
    }
}



using System;
 
class Program
{
    static void Main(string[] args)
    {
        // Single-dimensional array
        int[] singleDimensionalArray = new int[5]; // Declaration and initialization of a single-dimensional integer array
        singleDimensionalArray[0] = 10;
        singleDimensionalArray[1] = 20;
        singleDimensionalArray[2] = 30;
        singleDimensionalArray[3] = 40;
        singleDimensionalArray[4] = 50;
 
        // Multidimensional array
        int[,] multiDimensionalArray = new int[2, 3]; // Declaration and initialization of a 2D integer array
        multiDimensionalArray[0, 0] = 1;
        multiDimensionalArray[0, 1] = 2;
        multiDimensionalArray[0, 2] = 3;
        multiDimensionalArray[1, 0] = 4;
        multiDimensionalArray[1, 1] = 5;
        multiDimensionalArray[1, 2] = 6;
 
        // Jagged array (Array of arrays)
        int[][] jaggedArray = new int[3][]; // Declaration of a jagged array
        jaggedArray[0] = new int[] { 1, 2, 3 }; // Initialization of the first array in the jagged array
        jaggedArray[1] = new int[] { 4, 5 };    // Initialization of the second array in the jagged array
        jaggedArray[2] = new int[] { 6, 7, 8, 9 }; // Initialization of the third array in the jagged array
 
        // Outputting values from arrays
        Console.WriteLine("Single-dimensional array:");
        foreach (int num in singleDimensionalArray)
        {
            Console.Write(num + " ");
        }
        Console.WriteLine();
 
        Console.WriteLine("\nMulti-dimensional array:");
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.Write(multiDimensionalArray[i, j] + " ");
            }
            Console.WriteLine();
        }
 
        Console.WriteLine("\nJagged array:");
        foreach (int[] innerArray in jaggedArray)
        {
            foreach (int num in innerArray)
            {
                Console.Write(num + " ");
            }
            Console.WriteLine();
        }
    }
}