Reputation: 26689
So these two methods have the same signature but different constraints
public static void Method<T>(ref T variable) where T : struct { }
public static void Method<T>(ref T variable) where T : class { }
But they cannot be defined in a single class because they have the same signatures. But in this particular case they're mutually exclusive. (Unless I'm wrong about that)
I understand you can put additional constraints besides class
and struct
but you can't specify both struct
and class
on the same method. So why would this fail to compile?
Upvotes: 9
Views: 661
Reputation: 3493
They are semantically mutually exclusive, yes. But the compiler sees them as having the same "name", hence the ambiguity. "Name" here meaning "method signature".
Upvotes: 4
Reputation: 2918
Although the compiler could be smart enough to figure it out (which it appears not to be), you do not know what to do for object
(as it can be class
or struct
).
Upvotes: 4
Reputation: 498972
The generic constraints are not considered part of the method signature (thanks @Anthony for the link).
As far as the compiler is concerned you have a duplicate method - same numbers and types of parameters.
Upvotes: 10