2016-02-29 42 views
0

基本上是試圖縮短這個:C#如何重複一個動作

octree[nodeIndices[0]].nodes[nodeIndices[1]] 
    .nodes[nodeIndices[2]].nodes[nodeIndices[3]].nodes[nodeIndices[4]] 

我覺得這樣的事情可以工作,但不知道究竟如何:

octree[nodeIndices[0]].(Enumerable.Repeat(node[nodeIndices[i]], 4) 

編輯:這裏有更多的代碼

OctreeNode[] nodes = octree; 
ushort subdivisions = 1; 

while (true) 
{ 
    ushort nodeIndex = 0; 
    OctreeNode node = nodes[nodeIndex]; 

    if (node == null) 
    { 
     node = new OctreeNode(); 
     node.nodes = new OctreeNode[8]; 
     nodes = node.nodes; 
    } 

    subdivisions++; 

    if (subdivisions > 4) 
     break; 
} 

// the actual octree variable is unchanged 
+0

不幸的是,在C#中,沒有'With'就像我們在VB.Net中一樣。 –

+2

不知道'八叉樹'是什麼,我不認爲我們可以爲你做任何事 –

+2

你可以爲它寫一個循環或遞歸函數。 – BitTickler

回答

4
var result = octree[nodeIndices[0]]; 

for (int i = 1; i <= 4; i++) 
    result = result.node[nodeIndices[i]]; 

// do something with result; 
+0

如果我這樣做,那麼我不認爲我可以設置結果,並且我聽到指針在C#中不安全。 –

+0

@JimmyChristmas在這個答案中沒有指針。 – Tyress

+0

如果我設置了結果,它只會設置它的實例,而不是八叉樹內的實際成員。 –