C#C
C#3y ago
Hugh

❔ ✅ Generics 'where' : Limitations of what types are allows

When I'm specifying what types are allowed, is it possible to say anything about what types are not allowed.

For example, I've got 2 use cases here:

class A {}
class B : A {}
class A2 : A {}
class B2 : B {}


// I want to say that T1 is *not* allowed to be a subclass of B
void DoSomething<T1, T2>(T1 val1, T2 val2)
  where T1 : A
  where T2 : B
{}

// I want to say that T1 and T2 are not allowed to be the same type
void DoSomethingElse<T1, T2>(T1 val1, T2 val2)
  where T1 : A
  where T2 : A
{}


Are either of this possible in C# at compile time?
Was this page helpful?