Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Generics and method overloading

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Zach - 22 Jun 2007 17:53 GMT
Consider the following code:

void Test(int i)
{
  System.Console.WriteLine("int function");
}

void Test(object o)
{
  System.Console.WriteLine("object function");
}

void Generic<T>(T t)
{
  Test(t);
}

If I execute the code

Generic<int>(5);

then "object function" is printed to the screen.  I understand why, it
appears to treat generic types as first-class types and decides that
there must exist a compile-time conversion from EVERY legal value for
T (everything in this case, since there is no where clause) to the
overload.

My questions are:

1) is this theoretically necessary
2) What would be the implications of changing this so that the either
the correct overload is selected at runtime rather than at compile
time, or making it so that semantic checking on the code inside
Generic<T>() is done for individual values of T that the function is
instantiated with, and not for the "generic value T"?
Jon Skeet [C# MVP] - 22 Jun 2007 18:37 GMT
> Consider the following code:
>
[quoted text clipped - 22 lines]
> T (everything in this case, since there is no where clause) to the
> overload.

In particular, overloading is always performed at compile time, and the
void Generic<T>(T t) is *not* recompiled (in terms of IL) for the
Generic<int>(5); call. (It will be JIT-compiled separately, but that's
a different matter.)

> My questions are:
>
> 1) is this theoretically necessary

Well, anything can be done in theory, but this behaviour is consistent
with the general principle of overloading being done at compile-time
and overriding being done at runtime within .NET.

> 2) What would be the implications of changing this so that the either
> the correct overload is selected at runtime rather than at compile
> time, or making it so that semantic checking on the code inside
> Generic<T>() is done for individual values of T that the function is
> instantiated with, and not for the "generic value T"?

Selecting it at runtime would either require using reflection within
the compiled code, or changing the CLR to support overloading at
runtime.

The idea of the Generic<T>() code being checked for individual values
of T doesn't fly: you don't know all the values of T that will be used
when Generic<T> is compiled. This is where .NET generics are
significantly different from C++ templates, which are sort of (very)
glorified macros.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.