I am trying to add collection initialization to my class. I read about initializers here: https://msdn.microsoft.com/en-us/library/bb384062.aspx#Anchor_2
I will quote the important part that puzzles me:
Collection initializers allow you to specify one or more element initializers when initializing a collection class that implements IEnumerable or the class using the Add extension method.
Good, so I want to emphasize the word or . When I read it, I would have to create a class using the Add method, and then the collection initializer should work on this class? This does not seem to be the case. One thing I noticed was that it actually speaks of a method of adding an extension. So I tried to create the Add As Extension method, but to no avail.
Here is a small example that I tried that does not work:
public class PropertySpecificationCollection { private List<PropertySpecification> _internalArr; public void Add(PropertySpecification item) { _internalArr.Add(item); } }
Is citation subject to other interpretations than mine? I tried to read it again and again to see if I could interpret it in any other way, but I could not do it.
So, I guess that my question is: am I interpreting this incorrectly, am I missing something, or is there an error in the description of the collection initializers on MSDN?
source share