Inability to overload general methods with type restrictions

Possible duplicate:
General restrictions where T: struct and where T: class

Is there a special reason why you cannot overload common methods using mutually exclusive type restrictions in C #? For example, take these methods:

T DoSomething<T>(T arg) where T : class { /* Do something */ } T DoSomething<T>(T arg) where T : struct { /* Do something */ } 

and try calling them with

 DoSomething("1"); DoSomething(1); 

The way I see it, DoSomething () methods are mutually exclusive with respect to the parameters that they will take - the first accepts a reference type, the second accepts a value type. The compiler should be able to say that a DoSomething call with a string argument goes to the first method, and a DoSomething call with an int argument goes to the second method.

Am I missing something conceptually with generics here? Or is it just a function that has not been implemented in C #?

+8
generics c # method-overloading
source share
1 answer

General restrictions are not part of the method signature

See this answer. General restrictions on method overloading.

Jon Skeet Blog Post Related

+8
source share

All Articles