I have a piece of code, which outputs different results, depending on the C# compiler and the runtime.
The code in question is:
using System;
public class Program {
public static void Main() {
Console.WriteLine(string.Compare("alo\0alo\0", "alo\0alo\0\0", false, System.Globalization.CultureInfo.InvariantCulture));
}
}
The results are:
Compiling with mono (gmcs) Compiling with .Net (csc)
Running with mono -1 -1
Running with .Net -1 0
How can it output different values, when running with the .Net framework?
(BTW, according to http://msdn.microsoft.com/en-us/library/system.string.aspx the output should be 0, so mono's answer is incorrect, but that's unrelated to my question.)
Even the generated IL code is (almost) the same.
Compiling with .Net:
.method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 29 (0x1d)
.maxstack 8
IL_0000: nop
IL_0001: ldstr bytearray (61 00 6C 00 6F 00 00 00 61 00 6C 00 6F 00 00 00 ) // a.l.o...a.l.o...
IL_0006: ldstr bytearray (61 00 6C 00 6F 00 00 00 61 00 6C 00 6F 00 00 00 // a.l.o...a.l.o...
00 00 )
IL_000b: ldc.i4.0
IL_000c: call class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Globalization.CultureInfo::get_InvariantCulture()
IL_0011: call int32 [mscorlib]System.String::Compare(string,
string,
bool,
class [mscorlib]System.Globalization.CultureInfo)
IL_0016: call void [mscorlib]System.Console::WriteLine(int32)
IL_001b: nop
IL_001c: ret
} // end of method Program::Main
Compiling with mono:
.method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 27 (0x1b)
.maxstack 8
IL_0000: ldstr bytearray (61 00 6C 00 6F 00 00 00 61 00 6C 00 6F 00 00 00 ) // a.l.o...a.l.o...
IL_0005: ldstr bytearray (61 00 6C 00 6F 00 00 00 61 00 6C 00 6F 00 00 00 // a.l.o...a.l.o...
00 00 )
IL_000a: ldc.i4.0
IL_000b: call class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Globalization.CultureInfo::get_InvariantCulture()
IL_0010: call int32 [mscorlib]System.String::Compare(string,
string,
bool,
class [mscorlib]System.Globalization.CultureInfo)
IL_0015: call void [mscorlib]System.Console::WriteLine(int32)
IL_001a: ret
} // end of method Program::Main
The only difference is the two extra NOP instructions in the .Net version.
How is it possible? How can the two output values be different?
Also, if anyone has both .Net and mono installed, can you reproduce it?
EDIT: I don't care what the correct result is, and I don't care that mono and .Net produces different results. I'll probably never encounter embedded nulls AND sort them AND the sorting order will be important.
My problem is that the same runtime (.Net 2.0) produces different results, when compiled by different compilers.
EDIT 2: I added a table and tried to clarify the question, it should be easier to understand now.