473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Generics and method overloading

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"?

Jun 22 '07 #1
1 3052
Zach <di***********@gmail.comwrote:
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.
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<Tis compiled. This is where .NET generics are
significantly different from C++ templates, which are sort of (very)
glorified macros.

--
Jon Skeet - <sk***@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
Jun 22 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
5
by: Robby | last post by:
In C++ I can write template classes and in C# they have generic classes that are something like C++ template classes. Is there currently a why to write them in VB? MS is adding C++ type...
10
by: Pierre Arnaud | last post by:
I'd like to provide two generic methods with the same name and the same arguments, but with different constraints, such as: void Explore<T>(T a, T b) where T : struct { } void Explore<T>(T a, T...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
4
by: Random | last post by:
I want to define a generics method so the user can determine what type they expect returned from the method. By examining the generics argument, I would determine the operation that needs to be...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.