While answering a question i came upon this
StringCollection sc = new StringCollection();
sc.Add("Foo");
But this can be written as
StringCollection sc = new StringCollection() {"Foo"};
and this cannot be written
StringCollection sc = new StringCollection() {new string[] {"Foo"} };
That means Add method is called and AddRange is not.
How can i make a class that can have this functionality of having a default method called while creating its object?
It’s called a Collection Initializer. The class must implement IEnumerable and have a public
Addmethod.The class can have multiple
Addmethods, e.g.