Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组中的对象中的C#数组_C#_Arrays - Fatal编程技术网

数组中的对象中的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
私有成员,它在后台创建自己的成员来存储值,但是,您在尝试使用它之前没有初始化它。