views:

91

answers:

2

Hi,

I have a couple of classes all of them deriving from IQueryResult. I need each of those classes to be iterable in foreach loop. Unfortunately foreach loop cannot see GetEnumerator method. I've managed to use it in foreach using dynamic keyword available in .NET 4.0 but then IQueryResult need not to derive from IEnumerable.

public interface IQueryResult : IEnumerable<IQueryResult>
{
}

How would you do that in a more elegant way?

Kind Regards PK

A: 

a. dont inherit ineterfaces.

b. you can use an excpilcit cast to IEnumarable on that class and it will work

Hellfrost
Why do you say point a? What is wrong with inherting interfaces?
Iain
implementing an interface in a class is wonderfull.writing an interface to inherit another, is confusing a littel.and when you try to use them you need to cast stuff around, and it sucks.
Hellfrost
+1  A: 

Is an IQueryResult really IEnumerable<IQueryResult>? I might expect, for example, that (for a single grid):

public interface IQueryResult<T> : IEnumerable<T> { /* */ }

or if you are trying to represent multiple grids:

public interface IQueryResult<T> : IEnumerable<IEnumerable<T>> { /* */ }

Although I might be tempted to make it more explicit as a method:

public interface IQueryResult<T> {
    IEnumerable<IEnumerable<T>> GetGrids(); // or something
}

Note that you don't need IEnumerable[<T>] for foreach, but it makes life far more predictable, and makes it possible to use LINQ etc.

Marc Gravell