14

How do I convert from a IEnumerable variable to an int[] in variable in c#?

spender
  • 117,338
  • 33
  • 229
  • 351
Positonic
  • 9,151
  • 14
  • 57
  • 84
  • 5
    use the .ToArray() Extension method. – Johannes Rudolph Jul 04 '11 at 10:40
  • 1
    To the downvoter(s): to you it might be obvious, but does that merit a DV? The answers to most questions are obvious to somebody. – spender Jul 04 '11 at 10:43
  • 1
    @spender - As in the UK gameshow Who Wants to be a Millionaire? It's only easy if you know the answer! It's not a bad question - it's perfectly reasonable and 5 answers says its answerable. Might qualify for a dupe though. – Andras Zoltan Jul 04 '11 at 10:47
  • Yea seems a bit harsh since I did spend at least 30 minutes on Stackoverflow looking for an answer. Maybe they're having a bad day. – Positonic Jul 04 '11 at 10:49
  • 2
    @iKode - You have to learn to ignore the drive-by downvoters. They're an unfortunate part of the site. – Ritch Melton Jul 04 '11 at 11:01
  • @Rich Melton - Thanks Ritch, first time I've experienced this. Will do from now on :) – Positonic Jul 04 '11 at 13:08

5 Answers5

20

Use the .ToArray() extension method if you're able to use System.Linq

If you're in .Net 2 then you could just rip off how System.Linq.Enumerable implements the .ToArray extension method (I've lifted the code here almost verbatim - does it need a Microsoft®?):

struct Buffer<TElement>
{
    internal TElement[] items;
    internal int count;
    internal Buffer(IEnumerable<TElement> source)
    {
        TElement[] array = null;
        int num = 0;
        ICollection<TElement> collection = source as ICollection<TElement>;
        if (collection != null)
        {
            num = collection.Count;
            if (num > 0)
            {
                array = new TElement[num];
                collection.CopyTo(array, 0);
            }
        }
        else
        {
            foreach (TElement current in source)
            {
                if (array == null)
                {
                    array = new TElement[4];
                }
                else
                {
                    if (array.Length == num)
                    {
                        TElement[] array2 = new TElement[checked(num * 2)];
                        Array.Copy(array, 0, array2, 0, num);
                        array = array2;
                    }
                }
                array[num] = current;
                num++;
            }
        }
        this.items = array;
        this.count = num;
    }
    public TElement[] ToArray()
    {
        if (this.count == 0)
        {
            return new TElement[0];
        }
        if (this.items.Length == this.count)
        {
            return this.items;
        }
        TElement[] array = new TElement[this.count];
        Array.Copy(this.items, 0, array, 0, this.count);
        return array;
    }
}

With this you simply can do this:

public int[] ToArray(IEnumerable<int> myEnumerable)
{
  return new Buffer<int>(myEnumerable).ToArray();
}
Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
16

Call ToArray after a using directive for LINQ:

using System.Linq;

...

IEnumerable<int> enumerable = ...;
int[] array = enumerable.ToArray();

This requires .NET 3.5 or higher. Let us know if you're on .NET 2.0.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3
IEnumerable<int> i = new List<int>{1,2,3};
var arr = i.ToArray();
sehe
  • 374,641
  • 47
  • 450
  • 633
2
IEnumerable to int[] - enumerable.Cast<int>().ToArray();
IEnumerable<int> to int[] - enumerable.ToArray();
EgorBo
  • 6,120
  • 3
  • 35
  • 40
1
IEnumerable<int> ints = new List<int>();
int[] arrayInts = ints.ToArray();

Provided you are using Linq :)

Goblin
  • 7,970
  • 3
  • 36
  • 40