使用多变量选择变量

Kap*_*ein 1 c# variables

我有很多Int32我想要选择的变量int我要检查.是否可以使用多变量来生成此行并选择变量?

Int32 redleft0 = 0; 
Int32 redleft1 = 0; 
Int32 redleft2 = 0; 
Int32 redleft3 = 0; 
Int32 redleft4 = 0; 
Int32 redleft5 = 0;
Int32 blueleft0 = 0; 
Int32 blueleft1 = 0; 
Int32 blueleft2 = 0; 
Int32 blueleft3 = 0;     
Int32 blueleft4 = 0; 
Int32 blueleft5 = 0;

redorblue = "red";    
for (int i = 0; i < count; i++)
{ 
    String checkleftint = (redorblue + "left" + i);                   
    if (checkleftint < 0)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*rth 7

你应该使用一个数组 - 或者说两个 - 在这里:

var red = new int[]{0,0,0,0,0,0};
var blue = new int[]{0,0,0,0,0,0};

var arrayToUse = redorblue == "red" ? red : blue;
for (int i = 0; i < count; i++)
{
    var value = arrayToUse[i];
    // ....
}
Run Code Online (Sandbox Code Playgroud)