Whats the output of this code? [closed]
object nullobject = null;
object myobject = new myobject();
console.writeline("nullobject="+nullobject+"myobject="+myobject);
It (unexpectedly for me, anyway) prints
nullobject=myobject=System.Object
(changing your = new myobject();
line to = new object();
and correcting other typos.)
The thing I didn't know (and the reason I'm bothering to post this) is CSharp treats null string objects as empty strings when concatenating. There's a note halfway down this page about it. http://msdn.microsoft.com/en-us/library/ms228504.aspx
object nullobject = null;
object myobject = new object();
Console.WriteLine(nullobject + ""); //ok, prints empty line
Console.WriteLine(nullobject.ToString()); //this will blow up
Console.WriteLine("nullobject=" + nullobject + "myobject=" + myobject); //ok, prints what's above.
精彩评论