Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using collection initializer on my own class

I am trying to add collection initializing to my class. I read about the initializers here: https://msdn.microsoft.com/en-us/library/bb384062.aspx#Anchor_2

I'll quote the important part that puzzles me:

Collection initializers let you specify one or more element initializers when you initialize a collection class that implements IEnumerable or a class with an Add extension method.

Ok, so I want to emphasize on the word or. As I read it, I should be able to make a class with an Add method, and then the collection initializer should work on this class? This doesn't seem to be the case. One thing I did note, was that it does in fact say an Add extension method. So I tried creating the Add as an extension method as well, but to no avail.

Here's a small sample I tried that does not work:

public class PropertySpecificationCollection
{
    private List<PropertySpecification> _internalArr;
    public void Add(PropertySpecification item)
    {
        _internalArr.Add(item);
    }
}

Is the quote subject to other interpretations than mine? I tried reading it over and over again, to see if I could interpret it in any other way, but failed to do so.

So I guess my question is: Am I interpreting it wrong, am I missing something, or is there an error in the description of collection initializers on MSDN?

like image 688
Inrego Avatar asked Dec 20 '15 18:12

Inrego


People also ask

How do you initialize an object in a classroom?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

What is collection initializer?

C# 3.0 (. NET 3.5) introduced Object Initializer Syntax, a new way to initialize an object of a class or collection. Object initializers allow you to assign values to the fields or properties at the time of creating an object without invoking a constructor.

How do you initialize a class object in C#?

In object initializer, you can initialize the value to the fields or properties of a class at the time of creating an object without calling a constructor. In this syntax, you can create an object and then this syntax initializes the freshly created object with its properties, to the variable in the assignment.

How do you assign initial values to object properties when an object is created?

To create an object of a named class by using an object initializer. Begin the declaration as if you planned to use a constructor. Type the keyword With , followed by an initialization list in braces. In the initialization list, include each property that you want to initialize and assign an initial value to it.


1 Answers

It should be "and", not "or".

Collection initializers are described in C# language specification, section 7.6.10.3 Collection initializers:

The collection object to which a collection initializer is applied must be of a type that implements System.Collections.IEnumerable or a compile-time error occurs. For each specified element in order, the collection initializer invokes an Add method on the target object with the expression list of the element initializer as argument list, applying normal overload resolution for each invocation. Thus, the collection object must contain an applicable Add method for each element initializer.

It clearly states that the collection must implement IEnumerable and there needs to be an Add method. The call to the Add method is resolved using normal overload resolution process, so it can be an extension method, generic method etc.

like image 169
Jakub Lortz Avatar answered Oct 08 '22 09:10

Jakub Lortz