Array class in c# with example

Array class in c# with example


Introduction

In C#, arrays are fundamental structures for storing fixed-size sequential collections of elements of the same type. The Array class in the .NET framework provides various methods and properties to interact with these collections. This article delves into the Array class, its key features, and provides practical examples for a comprehensive understanding.

Understanding the Array Class in C#

The Array class is the base class for all arrays in C#. It's part of the System namespace and offers numerous methods and properties that make array manipulation easier and more intuitive.

Creating Arrays

Before exploring the Array class's functionalities, let's start with how to create arrays in C#.

int[] integerArray = new int[5]; // An integer array of size 5
string[] stringArray = { "Hello", "World", "Array", "Example" }; // A string array with 4 elements

Key Properties of the Array Class

  • Length: Gets the total number of elements in all dimensions of the Array.
  • Rank: Gets the number of dimensions of the Array.

Key Methods of the Array Class

  • Sort: Sorts the elements in an entire one-dimensional Array.
  • Copy: Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index.
  • IndexOf: Returns the index of the first occurrence of a value in a one-dimensional Array or in a portion of the Array.

Example: Utilizing Array Class Methods

Let's look at a simple example that demonstrates the use of some key methods of the Array class. 

using System;

class Program
{
    static void Main()
    {
        int[] numbers = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 4 };
        
        Console.WriteLine("Original array:");
        foreach (int i in numbers)
        {
            Console.Write(i + " ");
        }

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

        Console.WriteLine("\n\nSorted array:");
        foreach (int i in numbers)
        {
            Console.Write(i + " ");
        }

        // Finding an element index
        int index = Array.IndexOf(numbers, 9);
        Console.WriteLine("\n\nIndex of 9 in the array: " + index);
    }
}

Explanation:

  • We create an array of integers named numbers.
  • We use Array.Sort to sort the array.
  • We use Array.IndexOf to find the position of the number 9 in the array.

Working with Multi-dimensional Arrays

The Array class is also adept at handling multi-dimensional arrays.

Example: A Two-Dimensional Array 

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

Conclusion

The Array class in C# is a versatile and essential part of the .NET framework. It simplifies the task of managing collections of fixed-size elements, providing programmers with a powerful set of tools to manipulate arrays effectively.

Further Practice

Try using other methods like Array.Reverse, Array.Clear, or Array.Copy to deepen your understanding of the Array class in C#.

 

 

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