Extension "Add" methods are not considered in C# collection initializers - by Pavel Minaev [MSFT]

Status : 

  External<br /><br />
		This item may be valid but belongs to an external system out of the direct control of this product team.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 459307 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 5/27/2009 11:40:51 AM
Access Restriction Public


The C# language specification (version 3.0, which comes with both VS2008 and VS2010 beta1) has the following wording describing collection initializers (, "Collection initializers"):

"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."

This is underspecified, since it does not detail how the invocation takes place. It refers us to overload resolution; however, the overload resolution section (7.4.3, "Overload resolution") says:

"Overload resolution selects the function member to invoke in the following distinct contexts within C#:
•	Invocation of a method named in an invocation-expression (§
•	Invocation of an instance constructor named in an object-creation-expression (§
•	Invocation of an indexer accessor through an element-access (§7.5.6).
•	Invocation of a predefined or user-defined operator referenced in an expression (§7.2.3 and §7.2.4)."

Note that collection initializers aren't explicitly listed here. Presumably, collection initializer falls under the scope of the first context (invocation of a method). Looking at (, "Method invocations"), if no method that is a member of a class is applicable, extension methods for the type should be considered.

However, C# implementations in VS2008 and VS2010 don't seem to consider extension "Add" methods in collection initializers, even when such are otherwise in scope. See steps to reproduce for an example.

Sign in to post a comment.
Posted by JeroenHa on 5/3/2011 at 7:55 AM
Just a note: in VB.Net it IS supported. So also from a 'language parity' point of view, it would be nice if this would also be added to C#.
Posted by Louis.fr on 8/27/2010 at 6:49 PM
The specification explicitly states that an extension method can be used if the method call has one of four forms:

The collection initializer clearly doesn't use any of these.

This is not to say that it shouldn't be implemented, just to point out that that section of the specification should be updated if that feature is implemented.
Posted by Microsoft on 6/28/2010 at 5:27 PM
Thanks for the great suggestion for Visual Studio!

I've added a vote for collection initializers binding to Add extension methods to the OneNote notebook we use internally to track C# compiler and language requests. We can't promise if or when we'll get to this feature, but we'll definitely keep it in mind during planning for the next release!

Alex Turner
Program Manager
Visual Basic and C# Compiler
Posted by Microsoft on 6/16/2009 at 6:07 PM
Thanks for the suggestion for Visual Studio 2010!

You're right that the spec is ambiguous here! There was no explicit decision in C# 3.0 to prevent this from working; it was simply an implementation limitation we accepted when we discovered it late in the product cycle. We see no design reason not to add this now, but unfortunately, we are starting to lock down on our feature set and won't be able to get to this feature for this release. We'll definitely keep this in mind when planning for our next release!

Alex Turner
Program Manager
Visual C# Compiler
Posted by Microsoft on 5/29/2009 at 2:10 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com/)