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

If I do this in C # 4.0.NET

    private static void myMethod<T>(int obj) where T : IDictionary
    {


    }

    private static void myMethod<T>(int obj) where T : ICollection
    {


    }

I get the following error.

The type 'ConsoleApplication1.Program' already defines a member called 'myMethod' with the same types parameter

I would like to know why? As far as I can see, both of these methods can be called without ambiguity?

If I need the first method, I would do it

myMethod<IDictionary>(50)

and second method

myMethod<ICollection>(40)

What scenario am I missing? And is there a way to achieve a set of overloaded methods with the same parameters, but of a different type?

+3
source share
3 answers

. ( : ) . , ( ). , .

, , ++. , # ..Net generics ++ - .

; . , -.

, , , . - . . . , , , .

, , T, , . () .

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
}

new Dictionary new List , , .

private static void myMethodWithDictionary<T>(int obj) where T : IDictionary, new()
{
    // Create your new dictionary here, populate it, and set internal members
}

private static void myMethodWithCollection<T>(int obj) where T : ICollection, new()
{
    // Create your new collection here, populate it, and set internal members
}
+2

# .

#:

7.5.3

- -.

by @Anthony Pegram :

+9

, , public class Whatever : IDictionary, ICollection? , .

+1

All Articles