# Tuesday, 16 May 2006
« IKVM 0.28 rc1 | Main | IKVM 0.28 Released »
Using foreach to enumerate Java collections in C# 3.0
A long standing feature request for IKVM has been to make Java collections implement System.IEnumerable and I've always resisted doing that for various reasons.

I while ago -- while discussing this with Stuart Ballard -- I realised that with the proposed C# 3.0 language extensions, in particular "extension methods" it should be possible to make Java collections foreach-able with little effort:

namespace java.util
{
  public static class CollectionExtensionMethods
  {
    public static IEnumerator GetEnumerator(this java.util.Collection col)
    {
       return new CollectionEnumerator(col);
    }
  }
}

For those who don't know how extension methods work, basically the above code (note the this keyword before the argument type) adds a method to java.util.Collection that feels like an instance method.

Given the way foreach works, I had expected that it too would recognize this GetEnumerator method and therefore work on all Java collections, but alas that is not the case.
If you want to see this changed before C# 3.0 is released, please vote. Alternatively, if you're Miguel and on the ECMA C# committee, it should be obvious what to do ;-)

Tuesday, 16 May 2006 15:03:28 (W. Europe Daylight Time, UTC+02:00)  #    Comments [4]