Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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#_List - Fatal编程技术网

C# 如果为空,私有列表将返回新列表

C# 如果为空,私有列表将返回新列表,c#,list,C#,List,我有一个对象类,我正试图编写一个方法,返回这些对象的列表。该类具有以下属性 private string make { get; set; } private List<string> colours { get; set;} private List<string> trims { get; set; } 下面是我用来设置汽车列表的方法 public static List<Car> getCarLot() { List<Ca

我有一个对象类,我正试图编写一个方法,返回这些对象的列表。该类具有以下属性

private string make { get; set; }
private List<string> colours { get; set;}
private List<string> trims { get; set; }
下面是我用来设置汽车列表的方法

public static List<Car> getCarLot()
    {
        List<Car> carLot = new List<Car>();
        List<string> makeList = getMakes(); // returns a list of strings
        List<string> trimList = getTrims(); // returns a list of strings
        List<string> colorList = getColors(); // returns a list of strings

        foreach (string m in makeList)
        {
            Car car = new Car();
            car.colours = new List<string>(); // Want to remove this
            car.trims = new List<string>();
            car.make = m;
            foreach (string c in colorList)
                car.colours.Add(c);

            foreach (string t in trimList)
                car.trims.Add(t);

            carLot.Add(car);
        }

        return carLot;
    }

如何设置它,以便每次创建对象时不必将颜色和修剪设置为新的字符串列表

在Car类的构造函数中初始化属性:


您可以在类的构造函数中执行此操作:

public class Car
{
    private string make { get; set; }
    private List<string> colours { get; set;}
    private List<string> trims { get; set; }

    public Car()
    {
        colours = new List<string>();
        trims = new List<string>();
    }
}
或者,如果您使用的是C 6,即Visual Studio 2015,则可以直接初始化属性:

public class Car
{
    private string make { get; set; }
    private List<string> colours { get; set;} = new List<string>();
    private List<string> trims { get; set; } = new List<string>();
}
此外,您可以显著简化代码,而无需更改类:

public static List<Car> getCarLot()
{
    return makeList.Select(m => new Car
    {
        make = m,
        colours = colourList.ToList(),
        trims = trimList.ToList()
    });
}

由于您使用的是自动特性,因此有几个选择:

在C 6中,可以使用属性初始值设定项 在C 6之前,您需要在构造函数中设置属性 您可以将列表构造与填充结合起来 我会使用第三个选项,如下所示:

car.colours = colorList.ToList();
car.trims = trimList.ToList();
这使得foreach循环和构造函数调用都不必要,因为ToList在单个调用中构造并返回一个副本

public class Car
{
    private string make { get; set; }
    private List<string> colours { get; set;} = new List<string>();
    private List<string> trims { get; set; } = new List<string>();
}
public static List<Car> getCarLot()
{
    return makeList.Select(m => new Car
    {
        make = m,
        colours = colourList.ToList(),
        trims = trimList.ToList()
    });
}
car.colours = colorList.ToList();
car.trims = trimList.ToList();