2

I had to implement 2 interface same time with different generic parameter as below. I get confused enough about it. I had no idea which one of them iterate itself in foreach. Now i understand first one is implicitly choosen.

I have tried new BarList().GetEnumerator() but i can not specify type parameter on method level.

Only solution i have found it that casting it to interface like(new BarList() as IEnumerable<string>)

After confusing about it enough. I just wanted to know that this design is not really good idea ? I have to avoid to implement same generic interface one more time ?

class Program
{
    static void Main(string[] args)
    {
        foreach (var item in new BarList())
        {

        }
    }
}

class BarList: IEnumerable<string>, IEnumerable<int>
{

    public IEnumerator<int> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator<string> IEnumerable<string>.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

Edit:

Let me explain why i am going in this way.

I had to Implement IPagedList<T> interface which is inherited from IList<T>. I wanted to write extension method which convert it to My view model. like below

GetAll().ToPagedList(pageindex);//which is returning IPagedList Then i wanted to use it like below;

GetAll().ToPagedList(pageindex).ToViewModel<T,TViewModel>();

For achieve this I tried to return IPagedList<ViewModel> by that extension method.In that case I have to implement IPagedList 2 times with different parameter. But this strategy made confusing things. This is reason of it.

1
  • Can you post a link/code for IPagedList<T> or PagedList<T>? Oct 4, 2011 at 10:55

5 Answers 5

2

This seems a bit confusing. Why not make it explicit what is happening by adding the enumerators as properties rather than implementing them on the class. For example,

class ProductCollection 
{
   public IEnumerable<int> EnumerateTheInts { get { //code to produce enumerator }}
   public IEnumerable<string> EnumerateTheStringss { get { //code to produce enumerator }}
}

It isn't always bad to implement an open generic interface twice on an object. For example, IHandle could be implemented by a class which can handle two types of T. However, I would find it confusing to implement IEnumerable twice, because you might not enumerate the type you expect in a for-each or in LINQ. Same reasoning for implementing more than one indexer incidentally. The type of your indexer will determine your result, which I can testify to being extremely confusing!

2

The compiler is picking the IEnumerator<int> GetEnumerator method by following the rules in 8.8.4 of the C# language specification which first looks for an accessible GetEnumerator() method on the BarList type. The only one of those which is available is the one returning IEnumerator<int>.

If you had made that method use explicit interface implementation as well, then it would have gone onto the later stages of section 8.8.4, which states that if there is more than one type T such that there is an implicit conversion from the expression type (BarList here) to IEnumerable<T> then an error is produced.

I would say this is a confusing design - I would probably add properties or methods to retrieve appropriate "views" on the data.

1

I'd avoid it. However, it depends on your usage.

It will be okay if you just wanted to pass the instance into a function that expects a IEnumerable<string> parameter explicitely:

  • you won't have to cast
  • the function won't even 'see' the other interfaces implemented, so there isn't any confusion.

YMMV

1

Your current design is confusing. While you have not provided any information about the nature of the collection itself, from the name, I can assume you are supposed to iterate over a bunch of products. Perhaps, you should simply have a class of type Product with a string property and an int property and simply return an IEnumerable<Product> instead.

This way, with LINQ extension methods, you can compose the IEnumerable<T> object you actually mean with:

collection.Select(product => product.IntegerProperty)
collection.Select(product => product.StringProperty)

Of course, you can provide helper methods inside the object as well:

class ProductCollection : IEnumerable<Product> {
    public IEnumerable<Product> GetEnumerator() {
       // ... return Product objects here.
    }
    public IEnumerable<int> AsIntegerCollection() {
       // yield the integer collection here
    }
    public IEnumerable<string> AsStringCollection() {
       // yield the string collection here
    }
}
0
1

What are these collections of string and ints? I suppose they mean something in relation with the Product (for example Name, Id, etc...) so I would rather do something like this:

class ProductCollection : IEnumerable<Product>
{
    public IEnumerator<Product> GetEnumerator()
    {
       ...
    }

    public IEnumerator<string> ProductNames // a helper to enumerate product names
    {
       ...
    }

    public IEnumerator<int> ProductIds // a helper to enumerate product ids
    {
       ...
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.