Craig T Craig T - 27 days ago 6
C# Question

Debug.Print vs. Debug.WriteLine

What's the difference between

Debug.Print
and
Debug.WriteLine
? The summary of both in MSDN is the same:


Writes a message followed by a line terminator to the trace listeners in the Listeners collection.


Debug.WriteLine
has more overloads. I can't see a reason why
Debug.Print
would be used instead of
Debug.WriteLine
?

Answer

They both do the same thing, but its interesting that Debug.Print will only take a string, while Debug.WriteLine will accept an object which ends up calling the object's ToString method.

With 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);
}

I'd be willing to bet that Debug.Print was a hold over from Visual Basic.

Edit: From a tutorial on Tracing VB.NET Windows Application:

In Visual Basic.NET 2005, the Debug.Write, Debug.WriteIf, Debug.WriteLine, and Debug.WriteLineIf methods have been replaced with the Debug.Print method that was available in earlier versions of Visual Basic.

Sure sounds like Debug.Print was a hold over from pre-C# days.

Comments