[Mono-list] foreach and GetEnumerator

Rodolfo Campero rodolfocampero@hotmail.com
Mon, 04 Oct 2004 23:04:20 +0000


Hello,

I've run into a situation with foreachs and the GetEnumerator method.
The issue is reproduced with this small piece of code:

8<---------------------------------------------------
using System;
using System.Collections;

public interface IMyEnumerable {
        IEnumerator GetEnumerator();
}

public class TestCol : IEnumerable, IMyEnumerable {
        IEnumerator IEnumerable.GetEnumerator() {
                Console.WriteLine("IEnumerable");
                return null;
        }

        IEnumerator IMyEnumerable.GetEnumerator() {
                Console.WriteLine("IMyEnumerable");
                return null;
        }

        public static void Main(string[] args) {
                TestCol t = new TestCol();
                try {
                        foreach(object o in t) {}
                } catch(Exception) {}
        }
}
8<---------------------------------------------------

This program can be compiled using mcs, and when I run it I get:

        $ mono cols.exe
        IEnumerable

The problem is that this same piece of code won't compile in MS.NET csc
compiler (I checked my original program with version 1.1, but not this
simplified example): it complains that it can't choose between
System.Collections.IEnumerable and IMyEnumerable.

I'm rising this question because I'm not sure if this is a mono or a MS
.NET bug, because I find the spec (ECMA-334, 15.8.4) rather ambiguous.
At a given point it reads:

        "A type C is said to be a collection type if it implements
        "the System.IEnumerable interface or implements the collection
        "pattern by meeting all of the following criteria: [...]"

The thing is that it's not specified what happens when _both_ criteria
are met.

So, what do you think?

Looking forward to reading all your comments,

Rodolfo

_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*. 
http://join.msn.com/?page=features/featuredemail