This post originated from an RSS feed registered with .NET Buzz
by Michael Mello.
Original Post: Is there a method behind the madness?
Feed Title: melloblog
Feed URL: http://www.thauvin.net/errorpage.htm?aspxerrorpath=/Default.aspx
Feed Description: .NET and Everything After.
I had a discussion about Method Overloading today, and it seems that the definition and examples available are sometimes lacking, which leads to some confusion.
It seems that every example on the internet, and inside .NET books that I've come across, always show examples of Method Overloading with different parameters, but never with different modifiers or return types. The following is a vanilla example of Method Overloading that you might see:
Of course this code is perfectly fine, but what about different modifiers and return types? Even though I have trouble finding examples, this is perfectly legal?
Note: The modifier (in this case 'static') is used in only one of the methods.
public static int SweetMethod()
{
//do something
}
public int SweetMethod(int x)
{
//do something
}
?and so is this:
Note: The return types are different in this example. (int and string)
public int SweetMethod()
{
//do something
}
public string SweetMethod(int x)
{
//do something
}
Is the possibility of such a situation so far out in left field that most authors avoid it, or are these implementations of Method Overloading just, "Bad Practice"?