昨晚我做了一个梦,下面的事情是不可能的。但在同一个梦里,有人告诉我事实并非如此。因此,我想知道是否有可能转换系统。数组到列表

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

当前回答

我知道两种方法:

List<int> myList1 = new List<int>(myArray);

Or,

List<int> myList2 = myArray.ToList();

我假设您了解数据类型,并可以根据需要更改类型。

其他回答

你可以这样做:

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

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

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

你也可以对复杂对象这么做。

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

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

亲吻(简单点,先生)

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

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

... 对于数组类

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);

我知道两种方法:

List<int> myList1 = new List<int>(myArray);

Or,

List<int> myList2 = myArray.ToList();

我假设您了解数据类型,并可以根据需要更改类型。

我希望这对你有帮助。

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

获取字符串值

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);

获取整数值

        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);