Examining MSIL output for string operations on null object references
http://forums.asp.net/t/1234799.aspx
This was an interesting question to me as I had always known to test an object reference to be (Is Nothing) or != null before performing string operations or instance methods upon it... but I didn't know why. I also wanted to investigate exactly what the CLR does when the + operator is used on strings.
As an addendum to the above post, I discovered that using the & and + operators in VB is inherently sinful..
Console.WriteLine(obj1 + "asdf")
Console.WriteLine(" ")
Console.WriteLine(obj1 & "asdf")
produces the following IL:
IL_0002: ldloc.0
IL_0003: ldstr "asdf"
IL_0008: call object [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Operators::AddObject(object,
object)
IL_000d: call void [mscorlib]System.Console::WriteLine(object)
IL_0012: ldstr " "
IL_0017: call void [mscorlib]System.Console::WriteLine(string)
IL_001c: ldloc.0
IL_001d: ldstr "asdf"
IL_0022: call object [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Operators::ConcatenateObject(object,
object)
IL_0027: call void [mscorlib]System.Console::WriteLine(object)
I thought it was at least slightly interesting that VB.Net does not use the String.Concat function internally when the + operator is used. I expected & to be a VB CompilerServices method though.