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 ");
}