C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Array (Array)

In this tutorial, we'll demonstrate how to work with arrays in C#. An array is a fixed-size, indexed collection of elements of the same type. Arrays are helpful when you need to store and manipulate a collection of items in a program.

  • Set up the environment

Create a new C# Console Application project in Visual Studio.

  • Declare and initialize an array

Declare and initialize an array of integers with five elements:

int[] numbers = new int[5] { 10, 20, 30, 40, 50 };

Alternatively, you can use a shorter syntax to declare and initialize an array:

int[] numbers = { 10, 20, 30, 40, 50 };
  • Access elements in an array

Array elements are accessed using their index, which starts from 0. To access the first element in the numbers array, use the following syntax:

int firstNumber = numbers[0];
  • Loop through an array

You can use a for loop to iterate through the elements of an array:

for (int i = 0; i < numbers.Length; i++)
{
    Console.WriteLine($"Element {i}: {numbers[i]}");
}

Alternatively, you can use a foreach loop to iterate through the elements of an array:

foreach (int number in numbers)
{
    Console.WriteLine($"Element: {number}");
}
  • Multidimensional arrays

C# supports multidimensional arrays. To declare and initialize a two-dimensional array, use the following syntax:

int[,] matrix = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };

To access an element in a multidimensional array, use row and column indices:

int element = matrix[1, 0]; // This will get the value 3

You can use nested for loops to iterate through a two-dimensional array:

for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        Console.Write($"{matrix[i, j]} ");
    }
    Console.WriteLine();
}
  • Array methods

Arrays in C# have several useful methods, such as Sort(), Reverse(), and IndexOf(). For example:

int[] numbers = { 10, 20, 30, 40, 50 };

Array.Sort(numbers); // Sorts the array in ascending order
Array.Reverse(numbers); // Reverses the order of the elements in the array
int index = Array.IndexOf(numbers, 30); // Returns the index of the first occurrence of the value 30 in the array

In this tutorial, we've shown you how to work with arrays in C#. Arrays are a fundamental data structure in C# and provide an efficient way to store and manipulate a fixed-size collection of items of the same type.

  1. C# array example:

    • Description: This example provides a basic overview of arrays in C#, showcasing the declaration, initialization, and usage of a simple array.
    • Code:
      // Declare and initialize an array
      int[] numbers = { 1, 2, 3, 4, 5 };
      
      // Access and print elements of the array
      foreach (int number in numbers)
      {
          Console.WriteLine(number);
      }
      
  2. How to declare and initialize arrays in C#:

    • Description: This topic guides you through the process of declaring and initializing arrays in C#, introducing different syntax variations.
    • Code:
      // Declare and initialize an array using explicit size
      int[] numbers1 = new int[5];
      
      // Declare and initialize an array with initial values
      int[] numbers2 = { 1, 2, 3, 4, 5 };
      
  3. Accessing elements in a C# array:

    • Description: This example demonstrates how to access individual elements in a C# array using index notation.
    • Code:
      int[] numbers = { 1, 2, 3, 4, 5 };
      
      // Access and print specific elements
      Console.WriteLine(numbers[0]); // Output: 1
      Console.WriteLine(numbers[2]); // Output: 3
      
  4. Iterating through arrays in C#:

    • Description: Showcasing different methods for iterating through arrays in C#, such as using foreach and traditional for loops.
    • Code:
      int[] numbers = { 1, 2, 3, 4, 5 };
      
      // Using foreach loop
      foreach (int number in numbers)
      {
          Console.WriteLine(number);
      }
      
      // Using for loop
      for (int i = 0; i < numbers.Length; i++)
      {
          Console.WriteLine(numbers[i]);
      }
      
  5. Array length property in C#:

    • Description: Introducing the Length property to determine the size of an array in C#.
    • Code:
      int[] numbers = { 1, 2, 3, 4, 5 };
      
      // Print the length of the array
      Console.WriteLine($"Array length: {numbers.Length}");
      
  6. Sorting arrays in C#:

    • Description: This example demonstrates how to sort an array in ascending order using the Array.Sort method.
    • Code:
      int[] numbers = { 5, 3, 1, 4, 2 };
      
      // Sort the array in ascending order
      Array.Sort(numbers);
      
      // Print the sorted array
      foreach (int number in numbers)
      {
          Console.WriteLine(number);
      }
      
  7. Searching for an element in an array C#:

    • Description: Introducing methods for searching for an element in a C# array, such as using Array.IndexOf.
    • Code:
      int[] numbers = { 1, 2, 3, 4, 5 };
      int target = 3;
      
      // Find the index of the target element
      int index = Array.IndexOf(numbers, target);
      
      // Print the index or a message if not found
      Console.WriteLine(index != -1 ? $"Element found at index {index}" : "Element not found");
      
  8. Modifying elements in a C# array:

    • Description: This example illustrates how to modify elements within a C# array, showcasing the flexibility of array manipulation.
    • Code:
      int[] numbers = { 1, 2, 3, 4, 5 };
      
      // Modify the third element
      numbers[2] = 10;
      
      // Print the modified array
      foreach (int number in numbers)
      {
          Console.WriteLine(number);
      }
      
  9. Multidimensional arrays in C#:

    • Description: Introducing multidimensional arrays in C# with a focus on two-dimensional arrays.
    • Code:
      // Declare and initialize a 2D array
      int[,] matrix = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
      
      // Access and print elements of the 2D array
      Console.WriteLine(matrix[0, 1]); // Output: 2
      
  10. Passing arrays as parameters in C#:

    • Description: This example demonstrates how to pass arrays as parameters to methods in C#, enabling modular and reusable code.
    • Code:
      // Method that takes an array as a parameter
      static void PrintArray(int[] array)
      {
          foreach (int element in array)
          {
              Console.WriteLine(element);
          }
      }
      
      // Declare and initialize an array
      int[] numbers = { 1, 2, 3, 4, 5 };
      
      // Invoke the method with the array as a parameter
      PrintArray(numbers);