[Mono-bugs] [Bug 431453] New: internal GetEnumerator() methods should not be considered within a foreach()
bugzilla_noreply at novell.com
bugzilla_noreply at novell.com
Wed Oct 1 19:23:42 EDT 2008
https://bugzilla.novell.com/show_bug.cgi?id=431453
User jpryor at novell.com added comment
https://bugzilla.novell.com/show_bug.cgi?id=431453#c2
Summary: internal GetEnumerator() methods should not be
considered within a foreach()
Product: Mono: Compilers
Version: SVN
Platform: x86-64
OS/Version: openSUSE 11.0
Status: NEW
Severity: Normal
Priority: P5 - None
Component: C#
AssignedTo: mono-bugs at lists.ximian.com
ReportedBy: jpryor at novell.com
QAContact: mono-bugs at lists.ximian.com
Found By: ---
Consider the following source:
using System;
public class Enumerator
{
public bool MoveNext()
{
return false;
}
public int Current { get; set; }
}
public class Base
{
public Enumerator GetEnumerator()
{
return new Enumerator();
}
}
public class C : Base
{
new internal Enumerator GetEnumerator()
{
return new Enumerator();
}
}
class Test {
public static void Main ()
{
foreach (var e in new C())
Console.WriteLine (e);
}
}
Under CSC, compiling produces the following errors:
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
Documents\c.cs(33,9): warning CS0279: 'C' does not implement the 'collection'
pattern. 'C.GetEnumerator()' is either static or not public.
Documents\c.cs(24,29): (Location of symbol related to previous warning)
Documents\c.cs(33,9): error CS1579: foreach statement cannot operate on
variables of type 'C' because 'C' does not contain a public definition for
'GetEnumerator'
gmcs doesn't produce any error.
The reasoning is described in section 15.8.4 of the C# standard (4th edition),
"The foreach statement", page 237, major bullet #2, sub-bullet #2:
"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."
Because C.GetEnumerator() is internal, and because C doesn't implement
IEnumerable, then there is no .GetEnumerator() method which matches within the
scope of `foreach(var e in new C())`, so it should result in a failure.
--
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.
More information about the mono-bugs
mailing list