Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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

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

C# 如何在列表集合中初始化列表

C# 如何在列表集合中初始化列表,c#,c#-4.0,c#-3.0,C#,C# 4.0,C# 3.0,我有一个名为Item的类,它保存着一个Airpricepoint列表。我能够创建并初始化类的所有属性,并添加Airpricepoint列表。在Airpricepoint类中,我有另一个列表AirPricingInfo,它们的属性很少。如何访问AirPricingInfo的成员、初始化它们并将其添加到列表中 public class Item { public List<Airpricepoint> AirPricePoint { get; set; } } public c

我有一个名为
Item
的类,它保存着一个
Airpricepoint
列表。我能够创建并初始化类的所有属性,并添加
Airpricepoint
列表。在
Airpricepoint
类中,我有另一个列表
AirPricingInfo
,它们的属性很少。如何访问
AirPricingInfo
的成员、初始化它们并将其添加到列表中

public class Item
{
    public List<Airpricepoint> AirPricePoint { get; set; }
}

public class Airpricepoint
{
    public  List<Airpricinginfo> AirPricingInfo { get; set; }
    public string AirPricingResultMessage { get; set; }
    public string FeeInfo { get; set; }
    public string FareNote { get; set; }
    public string TaxInfo { get; set; }
    public string Key { get; set; }
    public string TotalPrice { get; set; }
    public string BasePrice { get; set; }
    public string ApproximateTotalPrice { get; set; }
    public string ApproximateBasePrice { get; set; }
    public string EquivalentBasePrice { get; set; }
    public string Taxes { get; set; }
    public string Fees { get; set; }
    public string Services { get; set; }
    public string ApproximateTaxes { get; set; }
    public string ApproximateFees { get; set; }
    public string CompleteItinerary { get; set; }
}

public class Airpricinginfo
{
    public object FareInfo { get; set; }
    public object FareStatus { get; set; }
    public IList<FareInfoRef> FareInfoRef { get; set; }
    public object BookingInfo { get; set; }
    public IList<TaxInfo> TaxInfo { get; set; }
    public string FareCalc { get; set; }
}

var lowfaresearchres = new Lowfaresearchres();
lowfaresearchres.Items= new Item();
lowfaresearchres.Items.AirPricePoint = new List<Airpricepoint>();    
lowfaresearchres.Items.AirPricePoint.Add(new Airpricepoint()
    {
        ApproximateBasePrice = airPricePoint.ApproximateBasePrice,
        ApproximateTotalPrice = airPricePoint.ApproximateTotalPrice,
        ApproximateTaxes = airPricePoint.ApproximateTaxes,
        ApproximateFees = airPricePoint.Fees,
        BasePrice = airPricePoint.BasePrice,
        Taxes = airPricePoint.Taxes,
        TotalPrice = airPricePoint.TotalPrice
    });
公共类项目
{
公共列表AirPricePoint{get;set;}
}
公共级机场点
{
公共列表AirPricingInfo{get;set;}
公共字符串AirPricingResultMessage{get;set;}
公共字符串FeeInfo{get;set;}
公共字符串{get;set;}
公共字符串TaxInfo{get;set;}
公共字符串密钥{get;set;}
公共字符串TotalPrice{get;set;}
公共字符串基价{get;set;}
公共字符串近似talprice{get;set;}
公共字符串{get;set;}
公共字符串等效BasePrice{get;set;}
公共字符串{get;set;}
公共字符串费用{get;set;}
公共字符串服务{get;set;}
公共字符串近似表{get;set;}
公共字符串近似项{get;set;}
公共字符串CompleteIntererary{get;set;}
}
公共类航空定价信息
{
公共对象信息{get;set;}
公共对象状态{get;set;}
公共IList FareInfoRef{get;set;}
公共对象BookingInfo{get;set;}
公共IList TaxInfo{get;set;}
公共字符串FareCalc{get;set;}
}
var lowfaresearchres=新的lowfaresearchres();
lowfaresearchres.Items=新项();
lowfaresearchres.Items.AirPricePoint=新列表();
lowfaresearchres.Items.AirPricePoint.Add(新AirPricePoint())
{
ApproximateBasePrice=airPricePoint.ApproximateBasePrice,
ApproximateTotalPrice=airPricePoint.ApproximateTalPrice,
ApproximateTaxes=airPricePoint.ApproximateTaxes,
近似费用=航空点费用,
BasePrice=airPricePoint.BasePrice,
税收=航空价格点。税收,
TotalPrice=airPricePoint.TotalPrice
});

我想你要问的是两个部分——如何初始化包含项的列表(不调用
Add
),以及如何初始化包含其他列表的列表(这实际上是相同的答案)

初始化任何对象内联的语法是在
newobjecttype
之后使用大括号
{}
,大括号内有一个逗号分隔的
PropertyName=Value
对列表

使用具有单个属性的类的一种简单形式是:

var person = new Person { Name = "Henry", Age = 25 };
使用单一列表的一种简单形式是:

var stringList = new List<string> { "one", "two", "three" };

var people = new List<Person>
{
    new Person {Birthday = DateTime.Now, Name = "Jack"},
    new Person {Birthday = DateTime.Parse("1/1/1980"), Name = "Jane"}
};
var stringList=新列表{“一”、“二”、“三”};
var people=新列表
{
新人{birth=DateTime.Now,Name=“Jack”},
新人{birth=DateTime.Parse(“1/1/1980”),Name=“Jane”}
};
如果使用嵌套列表,它将类似于:

var listOfListOfStrings = new List<List<string>>
{
    new List<string>{ "one", "two", "three" },
    new List<string>{ "four", "five", "six" }
};
var listofListofString=新列表
{
新名单{“一”、“二”、“三”},
新名单{“四”、“五”、“六”}
};
要填充对象,可以采用几种方法。一种方法是首先创建并初始化最内部的列表,然后在创建它们时将它们添加到父列表中:

// Create child lists
IList<FareInfoRef> fareInfoRef = IList<FareInfoRef>(); // And add some items
IList<TaxInfo> taxInfo  = new IList<TaxInfo>(); // And add some items

// Create the parent and add children
Airpricinginfo airPricingInfo = new Airpricinginfo 
{
    TaxInfo = taxInfo,
    FareInfoRef = fareInfoRef,
    // initialize other properties
}

// Continue with this pattern. . .
//创建子列表
IList fareInfoRef=IList();//并添加一些项目
IList taxInfo=新IList();//并添加一些项目
//创建父对象并添加子对象
Airpricinginfo Airpricinginfo=新的Airpricinginfo
{
TaxInfo=TaxInfo,
FareInfoRef=FareInfoRef,
//初始化其他属性
}
//继续这个模式。
但我想你要问的是,你怎么能把所有的事情都内联起来(这可能看起来有点混乱,但可能更简洁)

因此,将这个概念应用到对象中,您会得到类似的东西(注意,我没有编译这个,所以可能在某个地方有一个小错误):

lowfaresearchres.Items.AirPricePoint=新列表
{ 
新机场
{
AirPricingInfo=新列表
{
新航空信息
{
TaxInfo=新的IList(),
FareInfoRef=IList()
//添加其他AirPricingInfo属性
}, 
//添加更多由逗号分隔的Airpricinginfo对象
}
//添加其他Airpricepoint属性
},
//添加更多由逗号分隔的Airpricepoint对象
};

您知道如何访问列表中的项目吗?如果没有,请在C#/.NET中查找一些关于基本/基本集合的C#教程……您还可以让
构造函数为您实例化
列表
,这样您就可以从那里添加。
lowfaresearchres.Items.AirPricePoint = new List<Airpricepoint>
{ 
    new Airpricepoint
    {
        AirPricingInfo = new List<Airpricinginfo>
        {
            new Airpricinginfo
            {
                TaxInfo = new IList<TaxInfo>(),
                FareInfoRef = IList<FareInfoRef>()

                // Add other AirPricingInfo properties
            }, 
            // Add more Airpricinginfo objects separated by commas
        }

        // Add other Airpricepoint properties
    },
    // Add more Airpricepoint objects separated by commas
};