Method overloading by return type on C# is not permitted.

No.of Views1197
Bookmarked0 times
Downloads 
Votes0
By  helloraj   On  06 Jun 2010 11:06:14
Tag : CSharp , Miscellaneous
Method overloading by return type on C# is not permitted.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

I'm only writing about this because most people think that the signature concept is imposed by the CLR and this is not correct. If you see the behaviors of CLI you can see that it defines

* calling convention;
* number of generic parameters (if the method is generic);
* List of zero or more parameter signatures;
* type signature for return result .

In C# , multiple methods with same name is allowed if each method’s input parameter / parameter set is different which we say method overloading.

In C#, the signature of a method is formed by its name and its parameter set.. But one important point for you is that the return type of a method isn't included on its signature! The following list exemplifies this concept:

void HelloWorld();
void HelloWorld (int X); //This is Fine due to different parameter setvoid HelloWorld Method(int X,string Y);//Finevoid HelloWorld Method(string A, int B);//error same parameter set as previous methodint HelloWorld Method(int X); //error: can't overload based on return type only

As you can see, even though the last method has a different return type from the second one, it still causes a compile time error which says that there's already a member with that signature.

I believe this is. Because overloading on return type is fine in MSIL but not in practically everything else, changing a method name so that it creates an a new overload on return type means it can't be disassembled back into something like C#.

 

 
Sign Up to vote for this article
 
About Author
 
helloraj
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-23 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top