我对填充list<>
元素有疑问。我试图为list<>
对象的第一个元素设置一个值,但它不起作用。这是我的两门课:
班级
class hotel
{
public List<room> rooms = new List<room>();
}
class room
{
public string roomName;
}
Form1.cs
private void Form1_Load(object sender, EventArgs e)
{
string word = "example";
hotel[] h = new hotel[3];
for (int i = 0; i < h.Length; i++)
{
h[i] = new hotel();
h[i].rooms[i].roomName = word;//It gives "Index out of range exception" error.
}
}