I wondered if there's any type constraint in C# .NET that states that one type parameter doesn't equal another type parameter; something like this...
public class FooBar<TA,TB> where TA notequalto TB
The reason I want to do this is so that I can create 2 addition operators as per the following...
public class FooBar<TA,TB> where TA notequalto TB
{
public static TA operator +(TB b, FooBar<TA,TB> fooBar)
{
return fooBar.Foo(b);
}
public static TB operator +(TA a, FooBar<TA,TB> fooBar)
{
return fooBar.Bar(a);
}
public TA Foo(TB b) { ... }
public TB Bar(TA a) { ... }
}
The problem I face is that if TA = TB, it would be ambiguous in terms of which operator should be called, and thus whether Foo() or Bar() should be called! Although it's not essential, I wish to be able to implement this if possible...
No, there's no such constraint in C# - and I wouldn't expect there to be one in the future, given that it's of fairly limited use. (This isn't a situation that comes up terribly often in my experience.)
See more on this question at Stackoverflow