A strange question about overloading!

Source: Internet
Author: User
Tags mscorlib

 

Today, I found a strange problem when writing a reload. Let's look at such a program first!

 

Static void main (string [] ARGs)
{
Test (null );
}

Static void test (Object OBJ)
{
Console. writeline ("OBJ ");
}

Static void test (object [] OBJ)
{
Console. writeline ("OBJ array ");
}
Class Program
{
Static void main (string [] ARGs)
{
Test (null );
}

Static void test (Object OBJ)
{
Console. writeline ("OBJ ");
}

Static void test (object [] OBJ)
{
Console. writeline ("OBJ array ");
}
}

 

The program output is "OBJ array ".

However, why is test (null); automatically called?

Static void test (object [] OBJ)
{
Console. writeline ("OBJ array ");
}

 

What about this method?

 

I tried to open it with IL, and I didn't find any difference. The IL code is as follows:

. Method private hidebysig static void main (string [] ARGs) cel managed
{
. Entrypoint
// Code size 9 (0x9)
. Maxstack 8
Il_0000: NOP
Il_0001: ldnull
Il_0002: Call void consoleapplication1.program: Test (object [])
Il_0007: NOP
Il_0008: Ret

} // End of method program: Main

 

* ********** This is the main method ************

 

. Method private hidebysig static void test (object [] OBJ) cel managed
{
// Code size 13 (0xd)
. Maxstack 8
Il_0000: NOP
Il_0001: ldstr "OBJ array"
Il_0006: Call void [mscorlib] system. Console: writeline (string)
Il_000b: NOP
Il_000c: Ret
} // End of method program: Test

* ********** This is the ************* of the test (object [] OBJ) method ************

 

. Method private hidebysig static void test (Object OBJ) cel managed
{
// Code size 13 (0xd)
. Maxstack 8
Il_0000: NOP
Il_0001: ldstr "OBJ"
Il_0006: Call void [mscorlib] system. Console: writeline (string)
Il_000b: NOP
Il_000c: Ret
} // End of method program: Test

* ********** This is the ************** of the test (Object OBJ) method ************

 


. Method private hidebysig static void test (object [] OBJ) cel managed
{
// Code size 13 (0xd)
. Maxstack 8
Il_0000: NOP
Il_0001: ldstr "OBJ array"
Il_0006: Call void [mscorlib] system. Console: writeline (string)
Il_000b: NOP
Il_000c: Ret
} // End of method program: Test

 

Conclusion: The best solution is to add a overload without parameters. In this way, everything will be OK! (Thanks To qu bin*)

Static void main (string [] ARGs)
{
Test ();
}

Static void test ()
{
Console. writeline ("null ");
}

Static void test (Object OBJ)
{
Console. writeline ("OBJ ");
}

Static void test (object [] OBJ)
{
Console. writeline ("OBJ array ");
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.