Or seem to anyway.
=====
string abc;
if ((abc=getString()) != "")
{
MessageBox.Show(abc);
}
private string getString()
{
return "something";
}
=====
I get the MessageBox displayed. If I change != to == there's no
MessageBox.
What is the != comparing, and how?
Interesting question. Let's see what the compiler is up to by using ILDASM
to look at the generated IL code. I've put the code in the click event of
a button.
..method private hidebysig instance void button1_Click(object sender,
class [mscorlib]
System.EventArgs e) cil managed
{
// Code size 32 (0x20)
.maxstack 2
.locals init ([0] string abc)
//push the this pointer onto the stack
IL_0000: ldarg.0
//Stack now looks like:
// this
//load the return value of getString onto the stack
IL_0001: call instance string WindowsApplication5.Form1::getString
()
//Stack now looks like:
// this, "Something"
//Copy the top most value on the stack
IL_0006: dup
//Stack now looks like:
// this, "Something", "Something"
//Store the topmost value on the stack to
//the first local variable (abc in this case)
IL_0007: stloc.0
//Stack now looks like:
// this, "Something"
//Load an empty string onto the stack
IL_0008: ldstr ""
//Stack now looks like:
// this, "Something", ""
//Test the top two values on the stack for
//Inequality
IL_000d: call bool [mscorlib]System.String:

p_Inequality
string,string)
//Stack now looks like:
// this
//If False then jump to IL_001f
IL_0012: brfalse.s IL_001f
//Since the comparison of the top two stack values
//returns true you get the message box
//Load the string onto the stack
IL_0014: ldstr "Hello Something"
//Stack now looks like:
// this, "Hello Something"
//Show the message box
IL_0019: call valuetype [System.Windows.Forms]
System.Windows.Forms.DialogResult [System.Windows.Forms]
System.Windows.Forms.MessageBox::Show(string)
//Stack now looks like:
// this
//pop the this pointer off the stack
IL_001e: pop
//return
IL_001f: ret
} // end of method Form1::button1_Click
So, now you can see what is happening behind the scenes which should help
to explain what was going on in that expression and why it works.
Dave