Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.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#_Generics - Fatal编程技术网

C#构造函数使用带有两个参数的集合泛型

C#构造函数使用带有两个参数的集合泛型,c#,generics,C#,Generics,我正在创建一个接收地址和地址sqft的代码。我使用的是列表和集合。它只返回house+place六次。请帮忙 using System; using System.Collections.Generic; using System.Linq; class House { class place { public string address { get; set; } public int sqft { get; set; }

我正在创建一个接收地址和地址sqft的代码。我使用的是列表和集合。它只返回house+place六次。请帮忙

using System;
using System.Collections.Generic;
using System.Linq;

class House
{
    class place
    {
        public string address { get; set; }

        public int sqft { get; set; }

        public place(string address, int sqft)
        {
            this.address = address;
            this.address = address;
        }
    }

    public static void Main(string[] args)
    {
        List<place> places = new List<place>();


        places.Add(new place("lewis Rd.", 2001));
        places.Add(new place("mike Rd.", 1500));
        places.Add(new place("deseree St.", 1250));
        places.Add(new place("bottle Dr.", 2500));
        places.Add(new place("pen St.", 1100));
        places.Add(new place("walton St.", 999));

        for (int i = 0; i < places.Count; i++)
        {
            Console.Write(" {0}\n", places[i]);
        }

        Console.ReadKey();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
班房
{
班级
{
公共字符串地址{get;set;}
公共int sqft{get;set;}
公共场所(字符串地址,整数平方英尺)
{
this.address=地址;
this.address=地址;
}
}
公共静态void Main(字符串[]args)
{
列表位置=新列表();
地点。添加(新地点(“刘易斯路”,2001年));
地点。添加(新地点(“迈克路”,1500));
地点。添加(新地点(“应获得者街”,1250));
位置。添加(新位置(“瓶子博士”,2500));
地点。增加(新地点(“潘街”),1100);
地点。增加(新地点(“沃尔顿街”),999);
for(int i=0;i
我没有得到构造函数错误,但有两个问题需要解决

您的Console.Write行未指定要输出的属性,并且应该更接近此属性

Console.Write(" {0} has {1} square feet\n", places[i].address, places[i].sqft);
您还将分配两次地址

this.address = address;
this.address = address;
。。。当你可能想要的时候

this.address = address;
this.sqft = sqft;

可能这发生在您没有显示的其他代码中,或者您在遇到错误后没有重新编译,并且后来更改了代码。这里的代码应该可以毫无问题地编译……事实上,这段代码编译得很好——尽管我强烈建议您开始遵循.NET命名约定,请注意,构造函数两次分配给一个属性,而另一个则完全没有。您的代码运行良好,没有任何异常……很抱歉,我稍微更改了一下代码,然后对其进行了编译。我试图改变我的问题,但它不允许我改变。谢谢你的帮助。你说“它不允许我”是什么意思?当你尝试时发生了什么?您应该始终能够编辑您的问题。(顺便说一句,将来a会更好。)谢谢!!这正是我想要的。我在想这和我的班级有关。