C#, dynamic object names?

前端 未结 8 626
情歌与酒
情歌与酒 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.

提交回复
热议问题