Geeks With Blogs
Dheeman Dutta Just Another Blog.....

Just to add to my previous blog abt Generics Overloading the folowing scenarion is also very helpful.....

Suppose I have a class

public class TestClass<T, U>


public void Foo<I>(T val1, I val2, U val3) { }

public void Foo<I>(U val1, T val2, I val3) { }


And I'm calling this as

static void Main(string[] args)


TestClass<int, string> ob = new TestClass<int, string>();

ob.Foo<double>(1, 122223, "er");


it will compile pefectly as the permutations of U,T,and I are always unique....However if the call would be

static void Main(string[] args)


TestClass<int, int> ob = new TestClass<int, int>();

ob.Foo<double>(1, 122223, 2);



it would give a compile error.

Posted on Tuesday, September 12, 2006 9:29 AM | Back to top

Comments on this post: More abt Generics Overloading

# re: More abt Generics Overloading
Requesting Gravatar...
The compiler can generate an instance
TestClass<int,string>() because it's overloads would be
Foo<I>(int, I, string)
Foo<I>(string, int, I)
which easily meet the disambiguating rules for method signatures.

But when you try to do a TestClass<int,int>
the compiler starts building the Foo's:
Foo<I>(int, I, int) // ok.. firs one is fine
then then next foo,
Foo<I>(int, int, I) // oops!

At this point, the compiler can't guarantee parameter type disambiguity. Tha't because I is not instantiated and can't be verified.

Good compiler behavior! It *should* stop you from doing this. Method overload rules state that functions should differ by parameter types and number. Your overloads don't.

Moving 'I' around in the parameter order does little here because I is not in Test<T,U> class declaration.
Left by Nuri Halperin on Sep 12, 2006 7:06 PM

# re: More abt Generics Overloading
Requesting Gravatar...
This is exactly what i meant
thanks mate
Left by Dheeman on Sep 13, 2006 6:23 AM

Your comment:
 (will show your gravatar)

Copyright © Dheeman Dutta | Powered by: