数组中的对象中的C#数组
以下是我的代码的简化版本:数组中的对象中的C#数组,c#,arrays,C#,Arrays,以下是我的代码的简化版本: class House { private string owner; private int[] roomArea = new int[10]; public string Owner { get; set; } public int[] RoomArea { get; set; } } class Program { static void Main(string[] args) { Hou
class House
{
private string owner;
private int[] roomArea = new int[10];
public string Owner { get; set; }
public int[] RoomArea { get; set; }
}
class Program
{
static void Main(string[] args)
{
House[] london = new House[100];
for (int i = 0; i < 100; i++)
{
london[i] = new House();
}
london[0].Owner = "Timmy";
london[0].RoomArea[0] = 15; // Error points to this line
Console.WriteLine("Room 1 in " + london[0].Owner + "s house has the area of " + london[0].RoomArea[0] + "square meters");
}
}
我已经看过了,但是我不能确定我的代码到底出了什么问题。您需要初始化
RoomArea
。即使您在类内部初始化,它也会创建自己的成员,但是为了添加值,您需要初始化它
london[0].RoomArea = new int[10];
london[0].RoomArea[0] = 15;
您需要初始化
RoomArea
。即使您在类内部初始化,它也会创建自己的成员,但是为了添加值,您需要初始化它
london[0].RoomArea = new int[10];
london[0].RoomArea[0] = 15;
RoomArea
未初始化。RoomArea
属性没有使用RoomArea
私有成员,它在后台创建自己的成员来存储值,但是,您在尝试使用它之前没有初始化它。RoomArea
未初始化。RoomArea
属性没有使用RoomArea
私有成员,它在后台创建自己的成员来存储值,但是,您在尝试使用它之前没有初始化它。