C#基础:对象初始化器和集合初始化器

初始化器提供一种简洁的方式实例化和初始化对象。

  1.   public class Curry {
  2.         //自动属性
  3.         public string MainIngredient { get; set; }
  4.         public string Style { get; set; }
  5.         public int Spiciness { get; set; }
  6.         public Curry() { }//默认构造
  7.         public Curry(string mainIngredient, string style, int spiciness) {
  8.             MainIngredient = mainIngredient;
  9.             Style = style;
  10.             Spiciness = spiciness;
  11.         }
  12.         public Restaurant Origin { get;set; }    
  13.     }
  14.     public class Restaurant
  15.     {
  16.         public Restaurant() { }
  17.         public Restaurant(string name, string local, int rat)
  18.         {
  19.             Name = name;
  20.             Location = local;
  21.             Rating = rat;
  22.         }
  23.         public string Name { get; set; }
  24.         public string Location { get; set; }
  25.         public int Rating { get; set; }
  26.     }
  27. public static void Main(string[] args){
  28.           //对象初始化 常规方法(1)
  29.             Curry tastyCurry = new Curry();
  30.             tastyCurry.MainIngredient = "panir tikka";
  31.             tastyCurry.Style = "jalfrezi";
  32.             tastyCurry.Spiciness = 8;
  33.             //常规方法(2)
  34.             Curry curry = new Curry("hellokity", "kity", 8);
  35.             //方法(3)初始化器的使用任意方式初始化类
  36.             Curry currys = new Curry
  37.             {
  38.                 MainIngredient = "bybe",
  39.                 Style = "8888",
  40.                 Spiciness = 8,
  41.                 //初始化器的嵌套使用   属性的初始化器
  42.                 Origin = new Restaurant
  43.                 {
  44.                     Name = "张三",
  45.                     Location = "西安",
  46.                     Rating = 5
  47.                 }
  48.             };
  49.              //集合初始化
  50.              //List<Curry> curries = new List<Curry>();
  51.             //curries.Add(new Curry("Chicken","Pathia",6));
  52.             //curries.Add(new Curry("Vegetable","Korma",3));
  53.             //curries.Add(new Curry("Prawn","Vindaloo",9));
  54.             //集合初始化器的使用 可以替换上面的方式
  55.             List<Curry> curries = new List<Curry> {
  56.                 new Curry{
  57.                     MainIngredient="Chicken",
  58.                     Style="Pathia",
  59.                     Spiciness=6
  60.                 },
  61.                 new Curry{
  62.                     MainIngredient="Vegetable",
  63.                     Style="Korma",
  64.                     Spiciness=3
  65.                 },
  66.                 new Curry{
  67.                     MainIngredient="Prawn",
  68.                     Style="Vindaloo",
  69.                     Spiciness=9
  70.                 }
  71.             };
  72.             //用Var关键字修饰对象数组的初始化,称为匿名对象数组。
  73.              var currys = new[] {//匿名对象数组,var类型推理。
  74.                 new{MainIngredient = "bybe", Style = "bybyby",Spiciness = 8 }, 
  75.                 new{ MainIngredient = "haha", Style = "hahaha",Spiciness = 5},
  76.                 new{ MainIngredient = "haha", Style = "hahaha",Spiciness = 5}
  77.             };
  78. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值