2017-07-27 38 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(); 
相關問題