Generics: where T - TypeA OR TypeB
2 answers
It is not possible and it would not make sense. You can never rely on T
having specific members, because it could be A or B. All members are optional.
Of course, it is possible that this feature is there, but it goes against the spirit of generics. This would only be useful in a reflection situation.
+8
source to share
The only way to achieve this is to do TypeA
and TypeB
inherit or implement the same parent class or interface. For example:
public interface IParent
{
}
public class TypeA : IParent
{
//snip
}
public class TypeB : IParent
{
//snip
}
Then you can use
public class Blah<T> where T: IParent
{
}
+7
source to share