Reputation: 5200
When dealing with generics we can do where T: TypeA, TypeB
which means T must be implementing both TypeA and TypeB. But is it possible to use an OR in this expression? For example where T: TypeA || TypeB
? is there anyway to do this?
Upvotes: 3
Views: 131
Reputation: 118937
The only way to achieve this would be to make 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
{
}
Upvotes: 7
Reputation: 171188
That is not possible and it wouldn't really make sense. You could never rely on T
having certain members available because it could be A or B. All members are optional then.
It certainly is conceivable to have this feature but it goes against the spirit of generics. This would only be useful in a reflection situation.
Upvotes: 8