简体   繁体   English

转换 IEnumerable<int> 到 int[]</int>

[英]Convert IEnumerable<int> to int[]

How do I convert from a IEnumerable variable to an int[] in variable in c#?如何在 c# 中将 IEnumerable 变量转换为 int[] 变量?

Use the .ToArray() extension method if you're able to use System.Linq如果您能够使用 System.Linq,请使用.ToArray()扩展方法

If you're in.Net 2 then you could just rip off how System.Linq.Enumerable implements the.如果您在.Net 2 中,那么您可以扯掉 System.Linq.Enumerable 的实现方式。 ToArray extension method (I've lifted the code here almost verbatim - does it need a Microsoft®?): ToArray扩展方法(我已经几乎逐字逐句地解除了这里的代码 - 它需要 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();
}

Call ToArray after a using directive for LINQ:在 LINQ 的 using 指令之后调用ToArray

using System.Linq;

...

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

This requires .NET 3.5 or higher.这需要 .NET 3.5 或更高版本。 Let us know if you're on .NET 2.0.如果您使用的是 .NET 2.0,请告诉我们。

IEnumerable<int> i = new List<int>{1,2,3};
var arr = i.ToArray();
IEnumerable to int[] - enumerable.Cast<int>().ToArray();
IEnumerable<int> to int[] - enumerable.ToArray();
IEnumerable<int> ints = new List<int>();
int[] arrayInts = ints.ToArray();

Provided you are using Linq:)如果您使用的是 Linq :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换IEnumerable <int[][]> 到int [] [] - Convert IEnumerable<int[][]> to int[][] 将锯齿状数组转换为IEnumerable <KeyValuePair<int,int> &gt;? - Convert Jagged Array to IEnumerable<KeyValuePair<int,int>>? 如何转换IEnumerable <IEnumerable<IGrouping<int,string> &gt;&gt;到IEnumerable <IEnumerable<string> &gt; - How to convert IEnumerable<IEnumerable<IGrouping<int,string>>> to IEnumerable<IEnumerable<string>> 如何转换IEnumerable <IGrouping<int, DataRow> &gt;到DataTable? - How to convert IEnumerable<IGrouping<int, DataRow>> to DataTable? 转换现有的IEnumerable <int> 通过Method字符串(#,#,#,..) - Convert existing IEnumerable<int> to string (#,#,#,..) via Method 如何将IEnumerable <int>转换为Int32 - How do you convert IEnumerable<int> to an Int32 转换IEnumerable <Dictionary<int, string> &gt;列出 <Dictionary<int, string> &gt; - Convert IEnumerable<Dictionary<int, string>> to List<Dictionary<int, string>> 转换整数列表(IEnumerable<int> ) 到元组列表 (IEnumerable <IEnumerable<int> &gt;) 特定长度 - Convert list of integers (IEnumerable<int>) to list of tuples (IEnumerable<IEnumerable<int>>) of specific length 返回int []不能IEnumerable <int[]> 在LinqToXml中 - return int[] not IEnumerable<int[]> in LinqToXml 从int =&gt; IEnumerable转换lambda类型 <string> &gt;到int =&gt;任务 <IEnumerable<string> &gt;&gt;? - Convert lambda type from int => IEnumerable<string>> to int => Task<IEnumerable<string>>>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM