这是我的公开课:
public class CharactersOnline
{
public int connectionId;
public int characterId;
public string characterName;
}
这是我的代码:
public List<CharactersOnline> charactersOnline = new List<CharactersOnline>();
chraractersOnline[chraractersOnline.Count + 1].connectionId = 1;
chraractersOnline[chraractersOnline.Count + 1].characterId = 2;
chraractersOnline[chraractersOnline.Count + 1].characterName = "Test";
我的错误在哪里?为什么我无法将这些记录添加到列表中?
答案 0 :(得分:0)
你可以这样做(未经测试):
public List<CharactersOnline> charactersOnline = new List<CharactersOnline>();
var characterOnline = new CharactersOnline{connectionId = 1, characterId = 2, characterName = "test"}; //creates a new instance of your object
charactersOnline.Add(characterOnline); //adds the single new instance of your object to the list