Custom Collection Initializers
- by Jake
Classes that implement IEnumerable and provide a public void Add(/* args */) function can be initialized like in the following example:
List<int> numbers = new List<int>{ 1, 2, 3 };
which calls the Add(int) function 3x after initializing the List<int>.
Is there a way to define this behavior explicitly for my own classes? For example, could I have the initializer call a function other than the appropriate Add() overload?