How to create variables with dynamic names in C#?

前端 未结 6 1486
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 02:29

I want to create a var in a for loop, e.g.

for(int i; i<=10;i++)
{
    string s+i = \"abc\";
}

This should create variables s0, s1, s2... to

6条回答
  •  南笙
    南笙 (楼主)
    2021-01-25 03:09

    Obviously, this is highly dependent on the language. In most languages, it's flat-out impossible. In Javascript, in a browser, the following works:

    for (var i = 0; i<10 ; i++) { window["sq"+i] = i * i; }
    

    Now the variable sq3, for example, is set to 9.

提交回复
热议问题