A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 5.1: Arrays

In C#, arrays provide a way to store and manipulate collections of items of the same data type. This tutorial will cover the basics of working with arrays in C#.

1. Declaring and Initializing Arrays:

You can declare an array by specifying its data type, followed by square brackets [] and the array name. Here's a simple example:

using System;

class Program
{
    static void Main()
    {
        // Declaration and initialization of an integer array
        int[] numbers = { 1, 2, 3, 4, 5 };

        // Displaying array elements
        Console.WriteLine("Array Elements:");
        foreach (int num in numbers)
        {
            Console.Write(num + " ");
        }
    }
}

In this example, an integer array named numbers is declared and initialized with values { 1, 2, 3, 4, 5 }. The foreach loop is used to iterate through the array and display its elements.

2. Accessing Array Elements:

You can access individual elements of an array using their index. Remember that array indices in C# start at 0. Here's an example:

class Program
{
    static void Main()
    {
        // Declaration and initialization of a string array
        string[] fruits = { "Apple", "Banana", "Orange", "Grapes", "Mango" };

        // Accessing and displaying array elements
        Console.WriteLine("Fruit at index 2: " + fruits[2]);
    }
}

In this example, the string array fruits is initialized, and the element at index 2 ("Orange") is accessed and displayed.

3. Modifying Array Elements:

Arrays in C# are mutable, meaning you can change the values of their elements. Here's an example:

using System;

class Program
{
    static void Main()
    {
        // Declaration and initialization of an array
        int[] numbers = { 10, 20, 30, 40, 50 };

        // Modifying array elements
        numbers[2] = 35;

        // Displaying modified array elements
        Console.WriteLine("Modified Array Elements:");
        foreach (int num in numbers)
        {
            Console.Write(num + " ");
        }
    }
}

In this example, the value at index 2 of the numbers array is modified from 30 to 35.

4. Multi-Dimensional Arrays:

C# supports multi-dimensional arrays. The most common type is a 2D array. Here's an example:

using System;

class Program
{
    static void Main()
    {
        // Declaration and initialization of a 2D array
        int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

        // Displaying 2D array elements
        Console.WriteLine("2D Array Elements:");
        for (int row = 0; row < 3; row++)
        {
            for (int col = 0; col < 3; col++)
            {
                Console.Write(matrix[row, col] + " ");
            }
            Console.WriteLine(); // Move to the next row
        }
    }
}

This example initializes a 2D array named matrix and displays its elements using nested loops.

5. Array Length and Methods:

Arrays in C# have a Length property that indicates the number of elements in the array. Additionally, there are methods in the System.Array class, such as Sort() and IndexOf(), which can be used for array manipulation.

Here's an example using the Length property and the Sort() method:

using System;

class Program
{
    static void Main()
    {
        // Declaration and initialization of an integer array
        int[] numbers = { 5, 3, 8, 1, 7 };

        // Displaying original array elements
        Console.WriteLine("Original Array Elements:");
        foreach (int num in numbers)
        {
            Console.Write(num + " ");
        }

        // Sorting the array
        Array.Sort(numbers);

        // Displaying sorted array elements
        Console.WriteLine("\nSorted Array Elements:");
        foreach (int num in numbers)
        {
            Console.Write(num + " ");
        }

        // Displaying the length of the array
        Console.WriteLine("\nArray Length: " + numbers.Length);
    }
}

This example demonstrates sorting an array using Array.Sort() and getting the array length.

6. Experiment:

  • Create arrays of different data types.
  • Explore other methods in the System.Array class.
  • Try working with multi-dimensional arrays.

Congratulations! You've learned the basics of working with arrays in C#. In the next tutorial, we'll explore functions and methods in C#.