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)"

你可能感兴趣的:(C#)