Variables in a loop

前端 未结 9 1557
眼角桃花
眼角桃花 2020-12-19 18:32

I was wondering whether there\'s a way in a \"for\" loop to assign a value to a string variable named according to its index number?

let\'s say I have 3 string varia

9条回答
  •  猫巷女王i
    2020-12-19 19:01

    Usually instead of having N differents variables named 1, 2, ..., N the way is to store them in an array:

    string message[3];
    message[0] = null;
    message[1] = null;
    message[2] = null; 
    

    and then the loop:

    for (int i = 0; i <=2; i++)  
    {  
       message[i] = "blabla" + i.ToString();  
    }  
    

    Note that, usually again, a set of indexed variables starts with value 0 ;)

提交回复
热议问题