Search notes:

C#: collection initializer

A type that implements System.Collections.IEnumerable and provides an Add(xyz elem) method can be constructed with a so-called collection intializer:
TYPE coll = new TYPE { elem_1 , elem_2 , … elem_n } ;
Behind the scenes, the C# compiler turns that into something similar to
TYPE coll = new TYPE();
coll.Add(elem_1);
coll.Add(elem_2);
…
coll.Add(elem_n);
I try to demonstrate that with the following simple (and useless) type Coll:
using System;
using System.Collections;

class Coll : IEnumerable {

   public Coll() {
      Console.WriteLine("Constructor");
   }

   public void Add(int item) {
      Console.WriteLine($"adding {item}");
   }

   public IEnumerator GetEnumerator() {
   //
   // The method GetEnumerator() is required by IEnumerable.
   //
   // It is not required for this example, therefore, we
   // just return null:
   //
      return null;
   }
}

class Prg {

   static void Main() {
      Coll coll = new Coll {1, 1, 2, 3, 5, 8};
   }
}
//
// Example prints
//    Constructor
//    adding 1
//    adding 1
//    adding 2
//    adding 3
//    adding 5
//    adding 8
Github repository about-C-Sharp, path: /language/classes-structs/initializer/collection/requirements.cs

See also

object initializer

Index