2017-07-27 45 views
0

因此,例如,如果我有一个具有属性“距离”和“起源”的对象数组有没有任何快速的方法从该数组中获取距离属性的数组没有不必做:获取数组中的对象的属性

float[] distances = new float[objectArray.Length](); 
for (int i; i < objectArray.Length; i++) 
{ 
    distances[i] = objectArray[i].Distance; 
} 
+3

查找到'LINQ'。基本上,你可以写一些东西,比如'objectArray.Select(o => o.Distance).ToArray()'。 – Rob

回答

2

您需要使用LINQ查询投影如下图所示:

//use this namespace at the top of your code file 
using System.Linq; 

//inside your method. Replace the entire code in your post with this. 
var distances = objectArray.Select(x => x.Distance).ToArray();