[英]Debug.Print vs. Debug.WriteLine
Debug.Print
和Debug.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.Print
和Debug.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.