Languages
[Edit]
EN

C# / .NET - convert IEnumerable to array

0 points
Created by:
Marcino
720

In this article, we would like to show you how to convert IEnumerable to array in C#.

Quick solution:

IEnumerable<int> enumerable = new List<int> { 1, 2, 3 };

int[] array = enumerable.ToArray();

 

Practical example

In this example, we use ToArray() method to convert list of integers into array.

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        IEnumerable<int> enumerable = new List<int> { 1, 2, 3 };

        int[] array = enumerable.ToArray();

        foreach (int i in array)
        {
            Console.WriteLine(i);
        }
    }
}

Output:

1
2
3

References

  1. Enumerable.ToArray<TSource>(IEnumerable<TSource>) Method (System.Linq) | Microsoft Docs
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join