Would you like a to boost your class which hosts a collection, and utilize a cool C# language feature, which is collection initializer support?

For instance var myList = new List<int>() {1,3,4} where {1,3,4} is the initializer.

The basic idea is to implement IEnumerable<T>, and IEnumerator<T> and the class must have an ‘Add’ method with the correct signature.

I made the class generic so you can initialize any array (ok, not for structs and simple types, but you modify a few things and that works too)

// our sample class for iteration feeding

public class Address
   {
       public string Street { get; set; }
       public string StreetNumber { get; set; }
       public string ZipCode { get; set; }
       public string City { get; set; }
   }
   public static class Program
   {
       static void Main()
       {
           var t = new ClassWithInitializerSupport<Address> {
               new Address()
               {
                   Street = "Mainstr.",
                   StreetNumber = "1",
                   City ="London"
               },
              new Address()
               {
                   Street = "De Dam",
                   StreetNumber = "1",
                   City ="Amsterdam"
               },
               new Address()
               {
                   Street = "Mangostreet",
                   StreetNumber = "123",
                   City ="New York"
               }
           };
           foreach(var a in t)
           {
               Console.WriteLine("Street {0}, City {1}", a.Street, a.City);
           }

}

public class ClassWithInitializerSupport<T> : IEnumerable<T>, IEnumerator<T> where T : class
   {
       public ClassWithInitializerSupport()
       {
           arr = (T[])Array.CreateInstance(typeof(T), 0);
           pos = -1;
       }
       private T[] arr;
       private int pos;
       public T Current => arr[pos];

       object IEnumerator.Current => arr[pos];

       public void Dispose()
       {

       }

       public IEnumerator<T> GetEnumerator()
       {
           return this;
       }
       public void Add(T v)
       {
           var p = arr.Length;
           Array.Resize(ref arr, p + 1);
           arr[p] = v;
       }
       public bool MoveNext()
       {
           if (pos + 1 < arr.Length)
           {
               pos++;
               return true;
           }
           return false;
       }

       public void Reset()
       {
           pos = -1;
       }

       IEnumerator IEnumerable.GetEnumerator()
       {
           return this;
       }
   }