【发布时间】:2012-08-07 02:43:08
【问题描述】:
我有许多变量,例如
int foo1;
int foo2;
int foo3;
int foo4;
现在,我有一个 for 循环,从 var x = 0 到 3(对于 4 个变量),我想使用 x 来调用这样的变量:
for(int x = 0; x < 4; x++)
{
foo+x = bar;
}
这样当 x = 1 时,我的变量 foo1 将被赋值为 bar(当 x = 1 时,foo+x = bar == foo1 = bar)。
在 C# 中有什么方法可以做到这一点,或者我应该采取其他方法吗?
【问题讨论】:
-
有一种涉及反射的冗长方法,但您应该使用数组 (
int[]) 或List<int>。
标签: c# variables concatenation