Note that there are some explanatory texts on larger screens.

plurals
  1. POSame IL code, different output - how is it possible?
    text
    copied!<p>I have a piece of code, which outputs different results, depending on the C# compiler and the runtime.</p> <p>The code in question is:</p> <pre><code>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)); } } </code></pre> <p>The results are:</p> <pre><code> Compiling with mono (gmcs) Compiling with .Net (csc) Running with mono -1 -1 Running with .Net -1 0 </code></pre> <p>How can it output different values, when running with the .Net framework?</p> <p>(BTW, according to <a href="http://msdn.microsoft.com/en-us/library/system.string.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.string.aspx</a> the output should be 0, so mono's answer is incorrect, but that's unrelated to my question.)</p> <p>Even the generated IL code is (almost) the same.</p> <p>Compiling with .Net:</p> <pre><code>.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 </code></pre> <p>Compiling with mono:</p> <pre><code>.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 </code></pre> <p>The only difference is the two extra NOP instructions in the .Net version.</p> <p>How is it possible? How can the two output values be different?</p> <p>Also, if anyone has both .Net and mono installed, can you reproduce it?</p> <p>EDIT: <em>I don't care</em> what the correct result is, and <em>I don't care</em> that mono and .Net produces different results. I'll probably never encounter embedded nulls AND sort them AND the sorting order will be important.</p> <p>My problem is that the same <em>runtime</em> (.Net 2.0) produces different results, when <em>compiled</em> by different compilers.</p> <p>EDIT 2: I added a table and tried to clarify the question, it should be easier to understand now.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload