Why can't I overload a method with the same parameters if I choose different generic restraints?

Overloads are only based on function parameters. Return types and template parameters (more specifically: constraints) do not work for overloading a function. I can't tell you why this is (since I didn't write the language, and don't pretend to understand all their concerns).

I can only tell you what the compiler will allow One reason that you might want to overload on constraints might be to try to accomplish the equivalent of C++ template specialization. Unfortunately, C# doesn't support this. .

Net generics and C++ templates are extremely different beasts Regarding your specific example; there are two ways to solve this. Both require you to think about your problem differently The real problem is that the usage you are intending hurts your design. Overloading is really just syntactic sugar.

Overloading helps people call your public interface. It doesn't actually help you internally. In fact, if the overloaded methods do significantly different things, it can make it much harder to reason about your code while you are debugging it, and when you have to come back and maintain it Since the code that calls these functions has to specify T, you aren't saving any maintenance costs by overloading the function.

Instead, you could consider injecting the dependency (the collection) into these methods private static void myMethod(int obj, IDictionary dictionary) { // do something with the dictionary here, setting private members while you do it } private static void myMethod(int obj, ICollection collection) { // do something with the collection here, setting private members while you do it } If this solution would require you to duplicate your new Dictionary or new List calls too often, or if you want the method to control when the instance gets created, you can simply give up overloading private static void myMethodWithDictionary(int obj) where T : IDictionary, new() { // Create your new dictionary here, populate it, and set internal members } private static void myMethodWithCollection(int obj) where T : ICollection, new() { // Create your new collection here, populate it, and set internal members }.

Overloads are only based on function parameters. Return types and template parameters (more specifically: constraints) do not work for overloading a function. I can't tell you why this is (since I didn't write the language, and don't pretend to understand all their concerns).

I can only tell you what the compiler will allow. One reason that you might want to overload on constraints might be to try to accomplish the equivalent of C++ template specialization. Unfortunately, C# doesn't support this.. Net generics and C++ templates are extremely different beasts.

Regarding your specific example; there are two ways to solve this. Both require you to think about your problem differently. The real problem is that the usage you are intending hurts your design.

Overloading is really just syntactic sugar. Overloading helps people call your public interface. It doesn't actually help you internally.In fact, if the overloaded methods do significantly different things, it can make it much harder to reason about your code while you are debugging it, and when you have to come back and maintain it.

Since the code that calls these functions has to specify T, you aren't saving any maintenance costs by overloading the function. Instead, you could consider injecting the dependency (the collection) into these methods. Private static void myMethod(int obj, IDictionary dictionary) { // do something with the dictionary here, setting private members while you do it } private static void myMethod(int obj, ICollection collection) { // do something with the collection here, setting private members while you do it } If this solution would require you to duplicate your new Dictionary or new List calls too often, or if you want the method to control when the instance gets created, you can simply give up overloading.

Private static void myMethodWithDictionary(int obj) where T : IDictionary, new() { // Create your new dictionary here, populate it, and set internal members } private static void myMethodWithCollection(int obj) where T : ICollection, new() { // Create your new collection here, populate it, and set internal members }.

Overload resolution in C# doesn't take constraints into account at all. From the C# specification: 7.5.3 Overload resolution Overload resolution is a binding-time mechanism for selecting the best function member to invoke given an argument list and a set of candidate function members. The blog link by @Anthony Pegram is a much better read on the topic: Constraints are not part of the signature.

1 For more, see: blogs.msdn. Com/b/ericlippert/archive/2009/12/10/… – Anthony Pegram Mar 19 at 2:38 but why? Is this just a feature that hasn't been implemented or is there logical reasons for why it wouldn't work?

– stirfry Mar 19 at 2:38 @stickman, "why" and "why not" is a hotly debated topic (see the linked blog post and the comments). – Anthony Pegram Mar 19 at 2:41.

The compiler wouldn't know which overload to use.

But you'd know that at compile time and therefore in that situation the compiler can trap it as an error – stirfry Mar 19 at 2:39 In your example: if we made the call myMethod(50), the compiler wouldn't know which overload to use, since it implements both interfaces. – Bleaourgh Mar 19 at 2:41 yeah I understand the original situation. I'm saying that the compiler knows at compile time when the ambiguity will occur.So why not trap this situation as an error but allow situations when there's no ambiguity to proceed?

– stirfry Mar 19 at 2:44 @stickman: Because with your example, it will always fail. IDictionary inherits ICollection - msdn.microsoft.Com/en-us/library/… – Merlyn Morgan-Graham Mar 19 at 3:03.

Overloads are only based on function parameters. Return types and template parameters (more specifically: constraints) do not work for overloading a function. I can't tell you why this is (since I didn't write the language, and don't pretend to understand all their concerns).

I can only tell you what the compiler will allow. One reason that you might want to overload on constraints might be to try to accomplish the equivalent of C++ template specialization. Unfortunately, C# doesn't support this.

Net generics and C++ templates are extremely different beasts. Regarding your specific example; there are two ways to solve this. Both require you to think about your problem differently.

The real problem is that the usage you are intending hurts your design. Overloading is really just syntactic sugar. Overloading helps people call your public interface.

It doesn't actually help you internally. In fact, if the overloaded methods do significantly different things, it can make it much harder to reason about your code while you are debugging it, and when you have to come back and maintain it. Since the code that calls these functions has to specify T, you aren't saving any maintenance costs by overloading the function.

Instead, you could consider injecting the dependency (the collection) into these methods. If this solution would require you to duplicate your new Dictionary or new List calls too often, or if you want the method to control when the instance gets created, you can simply give up overloading.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions