« Using Yahoo! Maps GeoCoding API in C# | ^ Main | Free .Net Remoting? »


Constructor Overload

OK…I thought method overloading was sweet but constructor overloading makes it even better. The whole idea is you can have a method with the same name more than once with a different set of arguments for each. There are rules to how one is selected based on the conversion abilities of the arguments though.

Let’s start out with method overloading. Take for instance these functions.

public void Test(int blah)
{

}

public void Test(long blah)
{

}

public void Test(decimal blah)
{

}

Ok. By calling

sbyte anSbyte = 10;
Test(anSbyte)

which method will be chosen? Well, Rule One states the compiler will find the method with the argument closest to the argument passed on the conversion path. An sbyte (which is what gets passed) converts to a short, int, long, float, and decimal. The compiler chooses the closest which in this case would be int.

Moving on let’s use new methods:

public void Test(float blah)
{

}

public void Test(decimal blah)
{

}

Rule Two covers the possibility of a new issue. long can convert to float and decimal with no specific path between the two. In other words, there are two arrows that go from long. One goes to float and one to decimal so in steps Rule Two.

By calling

sbyte anSbyte = 10;
Test(anSbyte)

the compiler has ultimately starts at long since sbyte converts to (in this order) short, int, then long. So, which does the compiler choose? It doesn’t. The compiler spits on you. This isn’t only for sbyte but anything along the conversion path that ends up in a decision between float or decimal.

In my best attempt to convolute this post, C# can figure out, in an ambiguous situation like we just saw, which method to call. So, you ask “doesn’t that contradict what you just said?” No. Well, yes. Not exactly. lol. Rule Three covers this but in a little different light.

New methods:

public void Test(int blah)

public void Test(uint blah)

By calling
bc[CSharp].. byte mybyte = 10;
Test(mybyte);

the compiler again has to choose which method to use. byte can convert to short, int, long, float and decimal or ushort, uint, ulong, float and decimal. Wow…two conversion paths again. The difference here is in the function arguments. Rule Three informs us the compiler, when two equally suited signatures are found with one signed and the other unsigned, will always select the method with the signed argument. In this case, our method will call the first Test() method, with the int.

Once you get the concepts down I really think you can do some good with overloaded methods. The great part is constructors are methods too. So you can specify multiple constructors with the same concepts as above and your app will flow like water on a babies back.

public class Blah
{
public Blah()
{

}

public Blah(int x)
{

}

public Blah(string x)
{

}

public Blah(char x)
{

}
}

You get the idea of method overloading already so no need to repeat. Basically you have 4 constructors for 1 class which all operate different due to the parameter, or lack thereof, it receives. Pretty sweet huh?

I just wanted to share my excitement for C#’s abilities. There is more sweetness I just haven’t had time to blog about but I’ll get to it one day soon.

*Certain quotes, etc were directly derived from C# Primer Plus.

Posted by John C. Bland II on January 26, 2006 11:57 AM |

TrackBack

TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/13

Post a comment