implict type cast in generic method
Posted
by bitbonk
on Stack Overflow
See other posts from Stack Overflow
or by bitbonk
Published on 2010-05-04T14:00:19Z
Indexed on
2010/05/04
14:08 UTC
Read the original article
Hit count: 134
why do I get a compiler error in the following code stating: Cannot implicty convert type SpecialNode to T
even though T must derive from NodeBase as I defined in the where clause and even though SpecialNode actually derived from NodeBase?
public static T GetNode<T>() where T : NodeBase
{
if (typeof(T) == typeof(SpecialNode))
{
return ThisStaticClass.MySpecialNode; // <-- compiler error
}
if (typeof(T) == typeof(OtherSpecialNode))
{
return ThisStaticClass.MyOtherSpecialNode; // <-- compiler error
}
...
return default(T);
}
© Stack Overflow or respective owner