C#List <t>作为类</t>中的参数

时间:2015-03-25 18:43:35

标签: c# list class

我有一个问题,也许有人会帮助我做错的事情(或者你可以告诉我我可以在网上搜索什么?)

我有2个班级:

public class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Index { get; set; }
    public int Telephone { get; set; }
    public List<Adres> Adres { get; set; }

    public Student()
    {}
    public Student (string firstname, string lastname, int index, int telephone, List<Adres> adres)
    {
        FirstName = firstname;
        LastName = lastname;
        Index = index;
        Telephone = telephone;
        List<Adres> Adres = adres;
    }  
}
public class Adres
{
    public string Street { get; set; }
    public string City { get; set; }
    public int Code { get; set; }

    public Adres()
    { }
    public Adres(string street, int code, string city)
    {
        Street = street;
        City = city;
        Code = code;
    }
}

我的主项目文件中有两个新类

Adres _adres = new Adres ("street", code, "town");
Student _student = new Student("name", "surname", index, phone, List<Adres> _adres);

在我的_student课程中(这个在底部),我不知道如何将_adres作为学生班的参数......(因为它是一个列表)

任何? PLS?

错误是:

 Error  2   The best overloaded method match for 'BazyDanych.Student.Student(string, string, int, int, System.Collections.Generic.List<BazyDanych.Adres>)' has some invalid arguments   
Error   3   Argument 5: cannot convert from 'bool' to 'System.Collections.Generic.List<BazyDanych.Adres>'   
Error   4   Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments   
Error   5   'BazyDanych.Adres' is a 'type' but is used like a 'variable'    

2 个答案:

答案 0 :(得分:1)

您需要创建一个新的List<T>,就像任何其他类一样:

new List<Address>(new[] { _address })

List<T>碰巧有一个构造函数,它将一系列项目添加到列表中,这有助于。

答案 1 :(得分:1)

此处的问题是_adres不是List<Adres>。它只是Adres。您需要将其添加到列表中。类似的东西:

Adres _adres = new Adres ("street", code, "town");
var adresList = new List<Adres>();
adresList.Add(_adres);
Student _student = new Student("name", "surname", index, phone, adresList);