C#, dynamic object names?

前端 未结 8 586
情歌与酒
情歌与酒 2020-12-21 20:07

Suppose I have a list of objects

List DogList = new List();

and I want to automatically add objects to it, li

相关标签:
8条回答
  • 2020-12-21 21:06

    If you don't need them to have a name you could try this.

    DogList.Add(new dogClass());
    

    Otherwise you can't dynamically name variables like that. However, you could use a dictionary to associate the string "myDog1" etc to a value.

    0 讨论(0)
  • 2020-12-21 21:06
    for (int i = 0; i < 10; i++)
    {
        dogClass myDog = new dogClass();
        DogList.Add(myDog);
    }
    
    0 讨论(0)
提交回复
热议问题