How to create variables with dynamic names in C#?

前端 未结 6 1429
伪装坚强ぢ
伪装坚强ぢ 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:04

    Your first example wouldn't work in any language as you are trying to redefine the variable "i". It's an int in the loop control, but a string in the body of the loop.

    Based on your updated question the easiest solution is to use an array (in C#):

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

提交回复
热议问题