Sai A Sai A
Updated date Aug 09, 2023
In this blog, we will explore the various methods available in C# for converting IEnumerable to Array, and learn when to use each technique.
  • 2.3k
  • 0
  • 0

Introduction:

The IEnumerable and Array data structures are essential components for handling collections of data. While IEnumerable provides a flexible and efficient way to iterate over a sequence of elements, there are situations where you might need to convert it into an Array to leverage the benefits of array-based operations. In this blog, we will explore the various methods for converting IEnumerable to Array in C#.

Method 1: Using the ToArray() Extension Method

The ToArray() extension method is a straightforward and efficient way to convert an IEnumerable to an Array. It is part of the System.Linq namespace and offers a convenient syntax.

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        IEnumerable<int> numbers = Enumerable.Range(1, 5);
        int[] array = numbers.ToArray();

        foreach (var num in array)
        {
            Console.WriteLine(num);
        }
    }
}

Output:

1
2
3
4
5

The ToArray() method converts the IEnumerable sequence into an Array. It internally iterates over the elements of the IEnumerable and creates a new Array, populating it with the elements. This method is efficient and concise, making it a popular choice for such conversions.

Method 2: Using the LINQ ToArray() Method

LINQ (Language Integrated Query) is a powerful feature in C# that provides a convenient way to query and manipulate data. The ToArray() method is also available directly within LINQ queries.

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        IEnumerable<int> numbers = Enumerable.Range(1, 5);
        int[] array = (from num in numbers select num).ToArray();

        foreach (var num in array)
        {
            Console.WriteLine(num);
        }
    }
}

Output: 

1
2
3
4
5

In this method, the LINQ query projects each element of the IEnumerable sequence and then converts the result into an Array using the ToArray() method. While this method provides more expressive syntax, it essentially achieves the same outcome as the previous method.

Method 3: Using a Custom Conversion Loop

In some cases, you might want more control over the conversion process or need to perform additional transformations before creating the Array. In such scenarios, you can manually iterate over the IEnumerable and populate the Array.

using System;

class Program
{
    static void Main()
    {
        IEnumerable<int> numbers = GetNumbers();
        int[] array = ConvertToCustomArray(numbers);

        foreach (var num in array)
        {
            Console.WriteLine(num);
        }
    }

    static IEnumerable<int> GetNumbers()
    {
        // Custom logic to generate or fetch numbers
    }

    static int[] ConvertToCustomArray(IEnumerable<int> numbers)
    {
        int count = numbers.Count();
        int[] array = new int[count];
        int index = 0;

        foreach (var num in numbers)
        {
            array[index] = num;
            index++;
        }

        return array;
    }
}

Output:

1
2
3
4
5

This custom conversion method gives you the flexibility to perform additional operations on each element before populating the Array. It also showcases the manual control you have over the conversion process.

Conclusion:

Converting IEnumerable to Array is a common operation in C# programming, and there are multiple methods to achieve this goal. The ToArray() extension method and LINQ provide elegant and efficient ways for simple conversions, while custom conversion loops offer more control and flexibility. 

Comments (0)

There are no comments. Be the first to comment!!!