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

Found a very interesting thing reagarding Overloading in  Generics. Consider the following scenario

class MyClass<T,U,V>{

     public void Foo(T val1,T val2){};

     public void Foo(U val1,T val2){};

}

The method Foo will be treated as an overload..... even though all of T,U,V can be int.

But unfornutaely you will get the error during your call to any of the Foo.

 

 

Cheers.....

 

Posted on Monday, September 11, 2006 7:55 AM | Back to top


Comments on this post: Generics OverLoading

# re: Generics OverLoading
Requesting Gravatar...
Does this even compile? Can you post some context for use of such code?
Where do you find it likely or usefull to have such overloads unless generics constraints would be involved?

Left by Nuri Halperin on Sep 11, 2006 4:30 PM

# re: Generics OverLoading
Requesting Gravatar...
This will definitely compile... This code snippent is not for just use. If you look at the code you will find that though all the three parameters are int it complies. If the 3 paramerts are all int the signature of either Foo becomes Foo(int,int) . This same thing will not compile had it not been use of generics, but normal method overloading.

However you cannot call any of these methods as it becomes an ambiguous call.

I posted this becasue of the starnge behaviour.
Left by Dheeman on Sep 12, 2006 2:15 AM

Your comment:
 (will show your gravatar)


Copyright © Dheeman Dutta | Powered by: GeeksWithBlogs.net