C# Arrays

Introduction

Arrays in C# are used to store multiple values of the same type in a single variable. They are a collection of items stored at contiguous memory locations. Arrays are useful when you need to work with a collection of similar data types. They provide a way to manage and organize large amounts of data efficiently.

Types of Arrays

  1. Single-Dimensional Arrays
  2. Multi-Dimensional Arrays
  3. Jagged Arrays

1. Single-Dimensional Arrays

A single-dimensional array, also known as a linear array, is the simplest form of an array. It is a list of elements of the same type, stored in contiguous memory locations.

Syntax

dataType[] arrayName = new dataType[size];

Example

using System;

namespace SingleDimensionalArrayExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare and initialize an array
            int[] numbers = new int[5] { 1, 2, 3, 4, 5 };

            // Access and display array elements
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.WriteLine(

quot;Element at index {i}: {numbers[i]}"); } // Modify an array element numbers[2] = 10; Console.WriteLine(

quot;Modified element at index 2: {numbers[2]}"); } } } 

Output

Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
Modified element at index 2: 10

2. Multi-Dimensional Arrays

Multi-dimensional arrays are arrays of arrays. The most commonly used multi-dimensional array is the two-dimensional array, also known as a matrix or a table.

Syntax

dataType[,] arrayName = new dataType[rows, columns];

Example

using System;

namespace MultiDimensionalArrayExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare and initialize a 2D array
            int[,] matrix = new int[3, 3]
            {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
            };

            // Access and display array elements
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine();
            }

            // Modify an array element
            matrix[1, 1] = 10;
            Console.WriteLine(

quot;Modified element at [1,1]: {matrix[1, 1]}"); } } } 

Output

1 2 3
4 5 6
7 8 9
Modified element at [1,1]: 10

3. Jagged Arrays

A jagged array is an array of arrays, where each “sub-array” can be of different lengths. This provides flexibility in storing data structures that are not uniform in shape.

Syntax

dataType[][] arrayName = new dataType[size][];

Example

using System;

namespace JaggedArrayExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare and initialize a jagged array
            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 };

            // Access and display array elements
            for (int i = 0; i < jaggedArray.Length; i++)
            {
                Console.Write(

quot;Row {i}: "); for (int j = 0; j < jaggedArray[i].Length; j++) { Console.Write(jaggedArray[i][j] + " "); } Console.WriteLine(); } // Modify an array element jaggedArray[1][2] = 10; Console.WriteLine(

quot;Modified element at [1][2]: {jaggedArray[1][2]}"); } } } 

Output

Row 0: 1 2
Row 1: 3 4 5
Row 2: 6 7 8 9
Modified element at [1][2]: 10

Array Methods

C# provides several built-in methods to work with arrays, such as Sort, Reverse, IndexOf, and Copy.

Example: Array Methods

using System;

namespace ArrayMethodsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 5, 2, 9, 1, 5, 6 };

            // Sort the array
            Array.Sort(numbers);
            Console.WriteLine("Sorted array: " + string.Join(", ", numbers));

            // Reverse the array
            Array.Reverse(numbers);
            Console.WriteLine("Reversed array: " + string.Join(", ", numbers));

            // Find the index of an element
            int index = Array.IndexOf(numbers, 5);
            Console.WriteLine("Index of 5: " + index);

            // Copy the array
            int[] copiedArray = new int[numbers.Length];
            Array.Copy(numbers, copiedArray, numbers.Length);
            Console.WriteLine("Copied array: " + string.Join(", ", copiedArray));
        }
    }
}

Output

Sorted array: 1, 2, 5, 5, 6, 9
Reversed array: 9, 6, 5, 5, 2, 1
Index of 5: 2
Copied array: 9, 6, 5, 5, 2, 1

Conclusion

Arrays in C# are used for managing collections of data. By understanding single-dimensional, multi-dimensional, and jagged arrays, as well as utilizing built-in array methods, you can effectively organize and manipulate data in your applications. Arrays provide a way to store and access data efficiently, making them a fundamental concept in programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top