Generic type in generic collection

Posted by Brian Triplett on Stack Overflow See other posts from Stack Overflow or by Brian Triplett
Published on 2010-04-01T14:03:29Z Indexed on 2010/04/01 14:13 UTC
Read the original article Hit count: 336

Filed under:
|
|

I have generic type that looks like:

public class GenericClass<T, U> where T : IComparable<T>
{
    // Class definition here
}

I then have a collection of these instances. What is the cleanest way to pass through the type constraints?

public class GenericCollection<V> where V : GenericClass<T, U> // This won't compile
{
    private GenericClass<T, U>[] entries;

    public V this[index]
    {
        get{ return this.entries[index]; }
    }
}

Is there perhaps a better way to design this? I think that specifying

GenericCollection<T, U, V> where V : GenericClass<T, U> 

seems awkward. Might be my only option though....

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET