Weird generics compile error.

Posted by Jouke van der Maas on Stack Overflow See other posts from Stack Overflow or by Jouke van der Maas
Published on 2010-06-06T19:45:18Z Indexed on 2010/06/06 19:52 UTC
Read the original article Hit count: 338

Filed under:
|
|

I have two classes, a base class and a child class. In the base class i define a generic virtual method:

protected virtual Bar Foo<T>() where T : Bar {}

Then in my child class i try to do this:

protected override BarChild Foo() // BarChild inherits Bar
{ 
  return base.Bar<T> as BarChild() 
}

Visual studio gives this weird error:

The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Foo()'. There is no boxing conversion or type parameter conversion from 'T' to 'Bar'.

Repeating the where clause on the child's override also gives an error:

Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly

So what am i doing wrong here?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET