C#中的变量变量 [英] Variable Variables in C#
问题描述
嘿!,我在当天用Autohotkey编码并创建变量你会这样做:
Hey!, I coded in Autohotkey back in the day and to create variable you would do this:
ExampleVar = 1
Targetvar%ExampleVar% = 1
By这样做你会创建新的var Targetvar1 = 1
By doing that you would create new var Targetvar1 = 1
int i = 1;
while (i <= 10)
{
variable%i% = i;
Print(variable%i%);
i++;
}
//预期输出:
//变量:" variable1"输出:"1"
//变量:" variable2"输出:"2"
//变量:" variable3"输出:"3"
//Expected Output: //Variable: "variable1" Output: "1" //Variable: "variable2" Output: "2" //Variable: "variable3" Output: "3"
这就是我在C#中所做的事情,但%下划线为红色。
That's what i got up to in C# but the % are underlined red.
推荐答案
尝试以下不是变量但可以放入列表或字典
Try the following which are not variables but could be placed into a list or dictionary
var index = 1;
while (index <= 10)
{
Console.WriteLine(
" variable = {index}");
指数+ = 1;
}
"variable = {index}"); index += 1; }
然后阅读
字符串插值。
字典示例。
var values = Enumerable.Range(1, 10).Select(item => item).ToArray();
var dictionary = values.ToDictionary(v => v, v => true);
为什么以上,因为你无法创建变量" ;在飞行中"你想要的
Why the above, because you can't create variables "on the fly" like you want
这篇关于C#中的变量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!