[英]Conversion of System.Array to List
昨晚我梦见以下事情是不可能的。 但在同一个梦里,来自 SO 的人却告诉了我不同的说法。 因此我想知道是否可以将System.Array
转换为List
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);
至
List<int> lst = ints.OfType<int>(); // not working
给自己省点痛...
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.
也只能...
List<int> lst = new List<int> { 10, 20, 10, 34, 113 };
或者...
List<int> lst = new List<int>();
lst.Add(10);
lst.Add(20);
lst.Add(10);
lst.Add(34);
lst.Add(113);
或者...
List<int> lst = new List<int>(new int[] { 10, 20, 10, 34, 113 });
或者...
var lst = new List<int>();
lst.AddRange(new int[] { 10, 20, 10, 34, 113 });
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);
有趣的是,没有人回答这个问题,OP 没有使用强类型int[]
而是使用Array
。
您必须将Array
转换为它的实际情况,一个int[]
,然后您可以使用ToList
:
List<int> intList = ((int[])ints).ToList();
请注意, Enumerable.ToList
调用列表构造函数,该构造函数首先检查参数是否可以强制转换为ICollection<T>
(数组实现),然后它将使用更有效的ICollection<T>.CopyTo
方法而不是枚举序列。
最简单的方法是:
int[] ints = new [] { 10, 20, 10, 34, 113 };
List<int> lst = ints.ToList();
或者
List<int> lst = new List<int>();
lst.AddRange(ints);
如果您想将枚举数组作为列表返回,您可以执行以下操作。
using System.Linq;
public List<DayOfWeek> DaysOfWeek
{
get
{
return Enum.GetValues(typeof(DayOfWeek))
.OfType<DayOfWeek>()
.ToList();
}
}
你基本上可以这样做:
int[] ints = new[] { 10, 20, 10, 34, 113 };
这是你的数组,你可以像这样调用你的新列表:
var newList = new List<int>(ints);
您也可以对复杂对象执行此操作。
在 vb.net 中就这样做
mylist.addrange(intsArray)
或者
Dim mylist As New List(Of Integer)(intsArray)
你可以试试你的代码:
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;
然后你可以使用 anyVariable 作为你的代码。
我知道两种方法:
List<int> myList1 = new List<int>(myArray);
或者,
List<int> myList2 = myArray.ToList();
我假设您了解数据类型,并且会根据需要更改类型。
只需使用现有方法.. .ToList();
List<int> listArray = array.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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.