Chapter91:OverloadResolution

No Comments

Section91.1:BasicOverloadingExample
ThiscodecontainsanoverloadedmethodnamedHello:

classExample

{

publicstaticvoidHello(intarg)

{

Console.WriteLine(“int”);

}

publicstaticvoidHello(doublearg)

{

Console.WriteLine(“double”);

}

publicstaticvoidMain(string[]args)

{

Hello(0);

Hello(0.0);

}

}

WhentheMainmethodiscalled,itwillprint

int

double

Atcompile-time,whenthecompilerfindsthemethodcallHello(0),itfindsallmethodswiththenameHello.In thiscase,itfindstwoofthem.Itthentriestodeterminewhichofthemethodsisbetter.Thealgorithmfor determiningwhichmethodisbetteriscomplex,butitusuallyboilsdownto”makeasfewimplicitconversionsas possible”.
Thus,inthecaseofHello(0),noconversionisneededforthemethodHello(int)butanimplicitnumeric conversion is needed for the method Hello(double). Thus, the first method is chosen by the compiler.

InthecaseofHello(0.0),thereisnowaytoconvert0.0toanintimplicitly,sothemethodHello(int)isnoteven consideredforoverloadresolution.Onlymethodremainsandsoitischosenbythecompiler.
Section91.2:”params”isnotexpanded,unlessnecessary
Thefollowing program:

classProgram

{

staticvoidMethod(paramsObject[]objects)

{

System.Console.WriteLine(objects.Length);

}

staticvoidMethod(Objecta,Objectb)

{

System.Console.WriteLine(“two”);

}

staticvoidMain(string[]args)

{

object[]objectArray=newobject[5];

Method(objectArray);

Method(objectArray,objectArray);

Method(objectArray,objectArray,objectArray);

}

}

willprint:

5

two

3

ThecallexpressionMethod(objectArray)couldbeinterpretedintwoways:asingleObjectargumentthathappens to be an array (so the program would output 1because that would be the number of arguments, or as an array of arguments, given in the normal form, as though the method Methoddid not have the keyword params. In these situations, the normal, non-expanded form always takes precedence. So, the program outputs 5.

In the second expression, Method(objectArray,objectArray), both the expanded form of the first method and thetraditionalsecondmethodareapplicable.Inthiscasealso,non-expandedformstakeprecedence,sothe program prints two.
Inthethirdexpression,Method(objectArray,objectArray,objectArray),theonlyoptionistousetheexpanded formofthefirstmethod,andsotheprogramprints3.
Section91.3:Passingnullasoneofthearguments
If you have

voidF1(MyType1x){

//dosomething

}

voidF1(MyType2x){

//dosomethingelse

}

andforsomereasonyouneedtocallthefirstoverloadofF1butwithx=null,thendoingsimply

F1(null);

willnotcompileasthecallisambiguous.Tocounterthisyoucan do

F1(nullasMyType1);

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts
No Comments

Recent Posts

Leave a Comment