Why is the ambiguity with generics inconsistent, instead of raising errors?

Possible Duplicate:
General Methods and Method Overloading

Say I have a class like

class SortedList<K>
{
    public string this[int i] { get { return "a"; /* dummy sample code */ } }
    public string this[K key] { get { return "b"; /* dummy sample code */ } }
}

Now let's say that some dude decided to use it:

static string Test1<K>(K key) { return new SortedList<K>()[key]; }

The compiler resolves this call to overload K key.

Now compare this to the words

static string Test2(int key) { return new SortedList<int>()[key]; }  // whoops

where the compiler resolves this with overload int i.

Now, if some poor soul speaks Test1(0), he will get a different result if he says Test2(0), although the bodies look almost the same at first glance.

, .
, , .

? , , ( ) - ?

+5
2

, ? , "-" -...

, ? ? , , ... , , "test1" getter test2 getter...

"int" (, ), getter ...

0

, , , . , . .

, , a b. , , , , . ... - , , , . ... , . , ... , .

0

All Articles