使用对象初始值设定项如何在列表中添加值?

问题描述 投票:0回答:2
private PostDto MapIntegration(IntDto integ)
{
    return new PostDto
    {
        prop1 = "7",
        prop2 = "10",
        prop3 = true,
        prop4 = "EA Test",
        product_list.Add(integ) // ERROR This says the name product_list does not exist in current context
    };
}

以及当我们看PostDto

public class PostDto 
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public bool prop3 { get; set; }
    public string prop4 { get; set; }
    public List<IntDto> product_list { get; set; }
}
c# object .net-core generic-collections
2个回答
5
投票

您需要先初始化列表,然后再添加项目。尝试这样的事情:


1
投票

product_list未初始化。

© www.soinside.com 2019 - 2024. All rights reserved.