从 List<T> 转换到数组 T[] [英] Conversion from List<T> to array T[]
本文介绍了从 List<T> 转换到数组 T[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有将强类型 List
转换为相同类型的数组的简短方法,例如:List
到 MyClass[]
?
Is there a short way of converting a strongly typed List<T>
to an Array of the same type, e.g.: List<MyClass>
to MyClass[]
?
简而言之,我的意思是一个方法调用,或者至少比:
By short i mean one method call, or at least shorter than:
MyClass[] myArray = new MyClass[list.Count];
int i = 0;
foreach (MyClass myClass in list)
{
myArray[i++] = myClass;
}
推荐答案
尝试使用
MyClass[] myArray = list.ToArray();
这篇关于从 List<T> 转换到数组 T[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文