Mutual exclusive restrictions on two methods with the same signature

So these two methods have the same signature but different limitations

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 the same class because they have the same signature. But in this particular case, they are mutually exclusive. (If I am not mistaken in this)

I understand that in addition to class and struct you can add additional restrictions, but you cannot specify both struct and class in the same way. So why is this not compiling?

+6
source share
3 answers

general restrictions are not considered part of the method signature (thanks @Anthony for the link).

As for the compiler, you have a repeating method - the same numbers and types of parameters.

+8
source

They are semantically mutually exclusive, yes. But the compiler believes that they have the same "name", hence the ambiguity. "Name" here means "method signature."

+4
source

Despite the fact that the compiler may be smart enough to understand it (which seems to be wrong), you do not know what to do for an object (how can it be a class or struct ).

+2
source

Source: https://habr.com/ru/post/918126/


All Articles