Welcome to MSDN Blogs Sign in | Join | Help

C#: CIL supports overloading by return type

C# and most other languages that allow method overloading do not allow overloading by return type. What this means is that methods can be overloading only if they differ by parameters. So the following code will not compile as the methods differ only by return type

class Employee

{

//...

}

class Program

{

static int Func(Employee emp) {

return emp.Id;

}

static string Func(Employee emp) {

return emp.Name;

}

}

However, CIL does support overloading methods by return types, even though C#, VB does not . To implement convertion operator overloading C# compiler uses this feature (I know of one usage and I'm sure that there are more :) )

Conversion Operator Overloading and return types

In the following code we are overloading the conversion operator for Employee class so that the class can be easily converted (casted) to string or int.

class Employee

{

public string Name;

public int Id;

public static explicit operator int (Employee emp) {

return emp.Id;

}

public static explicit operator string (Employee emp) {

return string.Format("{0} ({1})", emp.Name, emp.Id);

}

}

Employee employee = new Employee("Abhinaba Basu", 23567);

Console.WriteLine((int)employee);

Console.WriteLine((string)employee);

Inspection of the Employee class in the assembly with ILDasm or reflector reveals that there are two methods in it which are generated by the compiler.

.method public hidebysig specialname static
string op_Explicit(class ConversionOperator.Employee emp) cil managed

.method public hidebysig specialname static
int32 op_Explicit(class ConversionOperator.Employee emp) cil managed

Both these methods are identical other than the return types (one returns a string the other an int). So CIL does support method overloading when the methods differ by return types.

 

Published Friday, October 07, 2005 8:23 PM by abhinaba
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: C#: CIL supports overloading by return type

Friday, October 07, 2005 10:36 AM by Gabe
C# is a statically-typed language, so all overloads are computed at compile time. Thus, there is no need for CIL support for overloading.

# re: C#: CIL supports overloading by return type

Saturday, October 08, 2005 1:48 AM by abhinaba
CIL does support this. By any support in CIL I mean the convention of function name generation is such that two functions are generated in the assembly which differ by return type ONLY. So the designer of another .NET language can use this fact and consume these method in the CIL produced by say C#.

If the support was not in CIL and only in the typing mechanism in C# and say it generated mangled name foo1 and foo2 for methods differing by return types then the other langauage also built on CIL would not have figured out that indeed these are two methods differing only in return types but it would have thought that these are two different methods altogether.

# re: C#: CIL supports overloading by return type

Saturday, October 08, 2005 2:20 AM by abhinaba
English is a foreign language to me. But even then in the previous comment I have broken all my previous records and typed out the longest sentence that takes up an entire paragraph. If you are able to make sense out of it let me know :). I think I'll write a compiler to convert AbhiEnglish to HumanUnderStandable English

# re: C#: CIL supports overloading by return type

Wednesday, October 12, 2005 2:37 AM by marcus
Abhinaba,
I just came across your blog and you have very interesting topics. I noticed that there is specialname tag(?) in the IL. I decompiled some of my classes and regular (non operators) methods did not have this tag. Do you know what it does (it a web search and couldn't find the answer? I am wondering if it is hint to the runtime to handle operators differently than other methods. This might be why operators can overload return types, but not regular methods.

Marcus

# re: C#: CIL supports overloading by return type

Thursday, October 13, 2005 4:53 AM by abhinaba
SpecialName tag is put in metadata by the compiler for methods that are "special" :)

So you'll see this tag for constructors, methods generated for Properties (get_* and set_*), overloaded operators, etc. I'll try to follow this up with a detailed post on specialname and rtspecialname. stay tuned...

# re: C#: CIL supports overloading by return type

Tuesday, August 22, 2006 3:05 AM by Vinit
It is supported if u change both the return type as well as parameters(type,numbers).However, if u change only the return type and not the parameters then it wud throw an error.

# re: C#: CIL supports overloading by return type

Tuesday, August 22, 2006 6:10 AM by abhinaba
Vinit that is not true. What you are saying is that in *C#* you need to change parameters as well. In CIL (Common intermediate language) AKA MSIL you *can* overload ONLY by return type

# re: C#: CIL supports overloading by return type

Thursday, April 26, 2007 9:57 AM by Rob

Isn't part of the reason (I could be wrong!) because if you have say, ClassA and ClassB both of which implement IFoo, then had the following:

public ClassA DoSomething()

{

return new ClassA();

}

public ClassB DoSomething()

{

return new ClassB();

}

and then tried to write:

IFoo x = DoSomething();

the compiler could/would get mightily confused...? I

# re: 戻り値の型のみが異なるため、お互いをオーバーロードすることはできません

Monday, May 19, 2008 1:04 AM by DHJJ [Hatsune's Journal Japan] blog

re: 戻り値の型のみが異なるため、お互いをオーバーロードすることはできません

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker