generic interface question

N

Ning Hu

Hello,

I have a question on why I am getting an error of "Cannot implicitly
convert type 'testGenerics.testCollection' to
'testGenerics.IMyCollection<System.IComparable>'. An explicit
conversion exists (are you missing a cast?)" for the following code.
Since, a Double implements IComparable, It should be implicitly
castable no?

interface IMyCollection<myType> where myType : IComparable
{

}

class testCollection : IMyCollection<Double>
{

}

class Program
{
static void Main(string[] args)
{
IMyCollection<IComparable> test = new testCollection();
}
}

Thanks,
Ning
 
M

Mattias Sjögren

Since, a Double implements IComparable, It should be implicitly
castable no?

No. There's no inheritance relation between GenericType<A> and
GenericType<B> even if A derives from or implements B.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top