Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Debug.WriteLine incorrectly format strings?

Tags:

c#

.net

I have the following Debug.WriteLine:

Debug.WriteLine("Metadata Version: {0}", version); // update: version is a string 

The output is:

2.0: Metadata Version: {0}

Why is the string formatted this way?

I didn't see anything in MSDN documentation that identifies reasoning behind this format. I have to do the following to get a correctly formatted output:

Debug.WriteLine(string.Format("Metadata Version: {0}", version)); 
like image 944
IAbstract Avatar asked Apr 02 '14 17:04

IAbstract


People also ask

What does debug WriteLine do?

Definition. Writes information about the debug to the trace listeners in the Listeners collection.

Where does debug WriteLine write to?

Debug. WriteLine writes to the debug output. You can see it in your debugger and save it from there.

Where does system diagnostics debug WriteLine go?

Diagnostics. Debug. WriteLine will display in the output window ( Ctrl + Alt + O ), you can also add a TraceListener to the Debug.


Video Answer


1 Answers

Since version is a string, you're hitting the overload of WriteLine that accepts a category as its second parameter.

While there are any number of hacks to get around this behavior (I'll include a few below, for fun) I would personally prefer your solution as the preferable way of clearly ensuring that the string is treated as a format string.

Some other hacky workarounds:

Debug.WriteLine("Metadata Version: {0}", version, ""); Debug.WriteLine("Metadata Version: {0}", (object)version); Debug.WriteLine("Metadata Version: {0}", new[] { version }); Debug.WriteLine("Metadata Version: {0}", version, null); 
like image 164
Servy Avatar answered Sep 23 '22 02:09

Servy