开发者

Whats the output of this code? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and 开发者_StackOverflow中文版 cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.
 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜