c# - object and collection initializer

Since the .net 4.0 there introduce the concept of object initializer and collection initializer. They are especially handy to cut the amount of code to write expiclity and delegate the work to Compiler. 

 

Object Initializer 

 

 

 

  class Cat
  {
    public int Age { get; set; }
    public string Name { get; set; }
  }

public class Program 
{
  public static void Main(string[] args) 
  {
       Cat cat = new Cat { Age = 10, Name = "Fluffy" };
  } 

}
 

 

 

In LINQ, where you can use the Object initializer with anonymous types, which alows you to do the projection and others quite easily.

 

 

      var pet = new { Age = 10, Name = "Fluffy" }; // it is especially useful in LINQ eury expression
 

 

 

 

Collection Initializer 

 

Collection initializer will allow you to initialize an array .e.g Cat[] or a List<Cat> quick easily. generally you can do 

 

 

      List<Cat> cats = new List<Cat>
      {
        new Cat{ Name = "Sylvester", Age=8 },
        new Cat{ Name = "Whiskers", Age=2 },
        new Cat{ Name = "Sasha", Age=14 }
      };
 

But if you like, you can use the more cannonical way of constructing collections with constructor parenthesis appended . 

 

      List<Cat> cats2 = new List<Cat>()
      {
        new Cat(){ Name = "Sylvester", Age=8 },
        new Cat(){ Name = "Whiskers", Age=2 },
        new Cat(){ Name = "Sasha", Age=14 }
      };
 

and if you are using an array, you can even omit the type in after the new keyword. 

 

      Cat[] cats3 = new []{ 
        new Cat(){ Name = "Sylvester", Age=8 },
        new Cat(){ Name = "Whiskers", Age=2 },
        new Cat(){ Name = "Sasha", Age=14 }
      };

 

 

Below is the full code that I used to test the full Object initializer and Collection Initializer things. 

 

 

namespace ObjectCollectionInitializer
{

  // Demo the use of Object initalizer
  // where you have the form of 
  //  Cat cat = new Cat { Age = 10, Age = "Fluffy" } ;
  class Cat
  {
    public int Age { get; set; }
    public string Name { get; set; }
  }




  public class Program
  {
    public static void Main(string[] args)
    {
      Cat cat = new Cat { Age = 10, Name = "Fluffy" };
      var pet = new { Age = 10, Name = "Fluffy" }; // it is especially useful in LINQ eury expression


      List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      // and you can still keep the () operator 
      List<int> digits2 = new List<int> { 0 + 1, 12 % 3, MakeInt() };


      // Demo the use of Collection initializer
      // the collection initializer is a syntax sugar that allows you to 
      // add a set of objects to a collection.
      // the basic form of a Collection Initializer is as follow.
      //    List<Cat> cats = new List<Cat> {
      //       new Cat { Age = 10, Name = "Fluffy" },
      //       ...
      //    }
      // the same is true for object 
      List<Cat> cats = new List<Cat>
      {
        new Cat{ Name = "Sylvester", Age=8 },
        new Cat{ Name = "Whiskers", Age=2 },
        new Cat{ Name = "Sasha", Age=14 }
      };
      // or if you prefer, you can use the cannonical way (with () )
      List<Cat> cats2 = new List<Cat>()
      {
        new Cat(){ Name = "Sylvester", Age=8 },
        new Cat(){ Name = "Whiskers", Age=2 },
        new Cat(){ Name = "Sasha", Age=14 }
      };

      // if the collection is an array and 
      // if it is the same type, you can omit the types as well 
      Cat[] cats3 = new []{ 
        new Cat(){ Name = "Sylvester", Age=8 },
        new Cat(){ Name = "Whiskers", Age=2 },
        new Cat(){ Name = "Sasha", Age=14 }
      };
    }


    public static int MakeInt()
    {
      return 0;
    }
  }
}
 

 

References: 

you can refer "Object and Collection Initializer (C# Programming Guide)"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值