繁体   English   中英

Debug.Print与Debug.WriteLine

[英]Debug.Print vs. Debug.WriteLine

Debug.PrintDebug.WriteLine什么区别? MSDN中两者的摘要是相同的:

将一个消息后跟一个行终止符写入Listeners集合中的跟踪侦听器。

Debug.WriteLine有更多的重载。 我看不出使用Debug.Print而不是Debug.WriteLine

它们都做同样的事情,但有趣的是Debug.Print只接受一个字符串,而Debug.WriteLine将接受一个最终调用该对象的ToString方法的对象。

使用Reflector:

[Conditional("DEBUG")]
public static void Print(string message){
    TraceInternal.WriteLine(message);
}

[Conditional("DEBUG")]
public static void WriteLine(string message){
    TraceInternal.WriteLine(message);
}

[Conditional("DEBUG")]
public static void WriteLine(object value)
{
    TraceInternal.WriteLine(value);
}

我愿意打赌Debug.Print是Visual Basic的Debug.Print

编辑:跟踪VB.NET Windows应用程序的教程:

在Visual Basic.NET 2005中,Debug.Write,Debug.WriteIf,Debug.WriteLine和Debug.WriteLineIf方法已替换为早期版本的Visual Basic中提供的Debug.Print方法。

当然, Debug.Print听起来像是在C#日子里。

Debug.PrintDebug.WriteLine都提供了一个带有单个字符串参数的重载。 在内部,这些单参数方法以相同的方式工作并调用此方法:

TraceInternal.WriteLine(message);

这两种方法的区别在于Debug.Print提供了一个带有两个参数的重载: Debug.Print(String, Object[]) Print重载可以显示格式化的字符串 Debug.WriteLine没有重载提供该功能,但您当然可以使用:

Debug.WriteLine(string.Format())

WriteLine是在.NET Framework 1.1和Print in Framework 2.0中引入的。 在我看来,Microsoft应该使用WriteLine作为方法的名称并添加WriteLine(String, Object[])重载,而不是添加Print方法。

他们不想冒险破坏认为两种过载都不明确的.NET语言。 它们非常接近,WriteLine(“foo”)可以匹配WriteLine(string)和WriteLine(string,params object [])。 C#有一个规则,但这是特定于该语言。

从原型:

public static void Print(string message);
public static void Print(string format, params object[] args);

public static void WriteLine(object value);
public static void WriteLine(string message);

看起来Print可以执行格式化字符串,WriteLine可以转储对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM