# Tuesday, May 16, 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, May 16, 2006 3:03:28 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [4]
Tuesday, May 16, 2006 4:45:27 PM (W. Europe Daylight Time, UTC+02:00)
On a related note, the workaround that was suggested to you by Microsoft looks uncannily like this code I wrote yesterday:

private IEnumerable<Adapters.FieldsRow> GetEnumerable(Adapters.FieldsDataTable table) {
foreach (Adapters.FieldsRow row in table) {
yield return row;

In my case, Adapters.FieldsRow is a row from the strongly-typed dataset returned from a TableAdapter (strongly typed data access layer) that is new in VS.Net 2005. I wanted a generic IEnumerable so I could populate a List<T> from it.

In my situation, there is another approach I could take - since VS.Net 2005 generates table adapters as partial classes, I can simply Mixin a new method onto FieldsDataTable. As partial classes are a compiler mechanism, it isn't really an option for ikvm.
Tuesday, May 16, 2006 5:07:41 PM (W. Europe Daylight Time, UTC+02:00)
After thinking about this for a while I'm not actually sure I agree with this feature request. I admit to not having checked the C# language spec for how foreach is actually defined, but to me as a casual user of the language I've always assumed it's defined to operate specifically on things that are IEnumerable - or IEnumerable<T> in 2.0. And even though you seem to be implying that it's not, the conceptual model of real users of the language isn't something to ignore even when it doesn't quite agree with the spec.

I've always been a little uncomfortable with things that are defined solely by naming conventions (part of the reason why I like C# properties so much better than JavaBeans ones) so the idea that "anything that happens to have a GetEnumerator method with the right signature" isn't something I'm hugely in favor of.

But given this preference I'm not exactly thrilled with the idea of adding even more places for foreach to look.

Besides, I have an ulterior motive. If Microsoft gives a clear "no" to this suggestion, then your clear and unarguable reason not to add the code we already came up with to make this work in IKVM *today* goes out the window. Not that you'll still not be resistant to the idea, but at least I again have a hope of trying to convince you ;)
Wednesday, May 17, 2006 8:04:12 AM (W. Europe Daylight Time, UTC+02:00)
C# and DLinq chats occur roughly every 2 weeks, if you're interested: http://msdn.microsoft.com/chats/#06_0518_MSDN_Dlinq

Incidentally, Mads is a pretty good person to talk with, in general: part of his sordid past can be found at http://www.daimi.au.dk/~madst/

Just for background, there is a tendency toward To* methods with ToQueryable() being the latest (there's also ToList, which appeared earlier). You can experiment around with the latest CTP to get a feel for it.
Keith J. Farmer
Tuesday, June 27, 2006 12:56:06 PM (W. Europe Daylight Time, UTC+02:00)
The "syntactic sugar" provided by the foreach method is very compelling, and if the effort involved is not insurmountable I would like to suggest that perseverance is a great virtue. If the challenge alone is not enough motivation, we will buy you beer and find spectacular entertainment of an exotic and sultry nature! (to motivate you)
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Comment (HTML not allowed)  

Live Comment Preview