You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(Originally reported in ECMA standardization; copied here at the suggestion of @MadsTorgersen as it may somewhat fall out of async enumerable work.)
When iterating over a type that has a custom GetEnumerator method, it is claimed that after looking up the method group, the next step (in the C# 5 spec section 8.8.4) is to:
Perform overload resolution using the resulting method group and an empty argument list. If overload resolution results in no applicable methods, results in an ambiguity, or results in a single best method but that method is either static or not public, check for an enumerable interface as described below. It is recommended that a warning be issued if overload resolution produces anything except an unambiguous public instance method or no applicable methods.
However, that appears not to quite be the case:
usingSystem;usingSystem.Collections.Generic;classTest{staticvoidMain(string[]args){foreach(varxinnewTest())// error CS1579: foreach statement cannot operate on variables of type 'Test' because 'Test' does not contain a public definition for 'GetEnumerator'{Console.WriteLine(x);}}publicIEnumerator<int>GetEnumerator(intcount=10){for(inti=0;i<count;i++){yieldreturni;}}}
Here overload resolution with an empty argument list should be fine - I can call new Test().GetEnumerator(). There isn't an empty parameter list, but that's not the same thing.
(The same problem occurs for params int[] values, so we've actually had an issue since C# 1.0!)
The text was updated successfully, but these errors were encountered:
Thanks for reporting this. Let me do a bar check with the compat council. Since this has been working for a long time and it is not harmful, it may just become a grandfathered compiler bug. (see update in next comment)
As a side note, this reminds me of how the Deconstruct method is found. There it seems we are stricter (see DeconstructMethodHasParams2, OutParamsDisallowed, and DeconstructMethodHasArglist2).
It may be good to try GetEnumerator with __arglist as well...
Ah, I'd misunderstood the bug report. The current behavior is this code fails to compile (I edited the description to include the compiler error), but you would expect it to succeed.
(Originally reported in ECMA standardization; copied here at the suggestion of @MadsTorgersen as it may somewhat fall out of async enumerable work.)
When iterating over a type that has a custom
GetEnumerator
method, it is claimed that after looking up the method group, the next step (in the C# 5 spec section 8.8.4) is to:However, that appears not to quite be the case:
Here overload resolution with an empty argument list should be fine - I can call
new Test().GetEnumerator()
. There isn't an empty parameter list, but that's not the same thing.(The same problem occurs for
params int[] values
, so we've actually had an issue since C# 1.0!)The text was updated successfully, but these errors were encountered: