C# System.Array 到 List 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1603170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:16:52  来源:igfitidea点击:

Conversion of System.Array to List

c#

提问by user193276

Last night I had dream that the following was impossible. But in the same dream, someone from SO told me otherwise. Hence I would like to know if it it possible to convert System.Arrayto List

昨晚我梦见以下是不可能的。但是在同一个梦中,SO 的某个人告诉我不同​​。因此我想知道是否可以转换System.ArrayList

Array ints = Array.CreateInstance(typeof(int), 5);
ints.SetValue(10, 0);
ints.SetValue(20, 1);
ints.SetValue(10, 2);
ints.SetValue(34, 3);
ints.SetValue(113, 4);

to

List<int> lst = ints.OfType<int>(); // not working

采纳答案by Dave

Save yourself some pain...

为自己省点痛...

using System.Linq;

int[] ints = new [] { 10, 20, 10, 34, 113 };

List<int> lst = ints.OfType<int>().ToList(); // this isn't going to be fast.

Can also just...

也可以只...

List<int> lst = new List<int> { 10, 20, 10, 34, 113 };

or...

或者...

List<int> lst = new List<int>();
lst.Add(10);
lst.Add(20);
lst.Add(10);
lst.Add(34);
lst.Add(113);

or...

或者...

List<int> lst = new List<int>(new int[] { 10, 20, 10, 34, 113 });

or...

或者...

var lst = new List<int>();
lst.AddRange(new int[] { 10, 20, 10, 34, 113 });

回答by Matthew Whited

There is also a constructor overload for List that will work... But I guess this would required a strong typed array.

List 还有一个构造函数重载可以工作......但我想这需要一个强类型数组。

//public List(IEnumerable<T> collection)
var intArray = new[] { 1, 2, 3, 4, 5 };
var list = new List<int>(intArray);

... for Array class

... 对于 Array 类

var intArray = Array.CreateInstance(typeof(int), 5);
for (int i = 0; i < 5; i++)
    intArray.SetValue(i, i);
var list = new List<int>((int[])intArray);

回答by Smith

in vb.net just do this

在 vb.net 中只需执行此操作

mylist.addrange(intsArray)

or

或者

Dim mylist As New List(Of Integer)(intsArray)

回答by Danny

The simplest method is:

最简单的方法是:

int[] ints = new [] { 10, 20, 10, 34, 113 };

List<int> lst = ints.ToList();

or

或者

List<int> lst = new List<int>();
lst.AddRange(ints);

回答by 13kudo

You can just give it try to your code:

你可以试试你的代码:

Array ints = Array.CreateInstance(typeof(int), 5);
ints.SetValue(10, 0);

ints.SetValue(20, 1);
ints.SetValue(10, 2);
ints.SetValue(34, 3);
ints.SetValue(113, 4);

int[] anyVariable=(int[])ints;

Then you can just use the anyVariable as your code.

然后你可以使用 anyVariable 作为你的代码。

回答by Tim Schmelter

Interestingly no one answers the question, OP isn't using a strongly typed int[]but an Array.

有趣的是没有人回答这个问题,OP 使用的不是强类型int[]而是Array.

You have to cast the Arrayto what it actually is, an int[], then you can use ToList:

您必须将 theArray转换为实际情况, an int[],然后您可以使用ToList

List<int> intList = ((int[])ints).ToList();

Note that Enumerable.ToListcalls the list constructorthat first checks if the argument can be casted to ICollection<T>(which an array implements), then it will use the more efficient ICollection<T>.CopyTomethodinstead of enumerating the sequence.

请注意,Enumerable.ToList调用列表构造函数首先检查参数是否可以强制转换ICollection<T>(数组实现),然后它将使用更有效的ICollection<T>.CopyTo方法而不是枚举序列。

回答by Rahbek

In the case you want to return an array of enums as a list you can do the following.

如果您想将枚举数组作为列表返回,您可以执行以下操作。

using System.Linq;

public List<DayOfWeek> DaysOfWeek
{
  get
  {
    return Enum.GetValues(typeof(DayOfWeek))
               .OfType<DayOfWeek>()
               .ToList();
  }
}

回答by Арсений Савин

I hope this is helpful.

我希望这是有帮助的。

enum TESTENUM
    {
        T1 = 0,
        T2 = 1,
        T3 = 2,
        T4 = 3
    }

get string value

获取字符串值

string enumValueString = "T1";

        List<string> stringValueList =  typeof(TESTENUM).GetEnumValues().Cast<object>().Select(m => 
            Convert.ToString(m)
            ).ToList();

        if(!stringValueList.Exists(m => m == enumValueString))
        {
            throw new Exception("cannot find type");
        }

        TESTENUM testEnumValueConvertString;
        Enum.TryParse<TESTENUM>(enumValueString, out testEnumValueConvertString);

get integer value

获取整数值

        int enumValueInt = 1;

        List<int> enumValueIntList =  typeof(TESTENUM).GetEnumValues().Cast<object>().Select(m =>
            Convert.ToInt32(m)
            ).ToList();

        if(!enumValueIntList.Exists(m => m == enumValueInt))
        {
            throw new Exception("cannot find type");
        }

        TESTENUM testEnumValueConvertInt;
        Enum.TryParse<TESTENUM>(enumValueString, out testEnumValueConvertInt);

回答by Drew Aguirre

Just use the existing method.. .ToList();

只需使用现有的方法..ToList();

   List<int> listArray = array.ToList();

KISS(KEEP IT SIMPLE SIR)

吻(保持简单,先生)

回答by nzrytmn

You can do like this basically:

你基本上可以这样做:

int[] ints = new[] { 10, 20, 10, 34, 113 };

this is your array, and than you can call your new list like this:

这是你的数组,然后你可以像这样调用你的新列表:

 var newList = new List<int>(ints);

You can do this for complex object too.

您也可以对复杂对象执行此操作。