Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incorrect output from Debug.WriteLine("Put text here: {0}", myString)

String.Format works fine with Debug.WriteLine if variable not of string type:

int myNumber = 1;
Debug.WriteLine("Put number here: {0}", myNumber);

Correct Output with Non-String

  • Put number here: 1

But if variable is string:

string myString = "ShouldWork";
Debug.WriteLine("Put text here: {0}", myString);

Incorrect Output with String

  • ShouldWork: Put text here: {0}

Why?

like image 349
Cel Avatar asked Nov 13 '11 14:11

Cel


3 Answers

You are getting the wrong overload...

You could work around that:

Debug.WriteLine("Put text here: {0}", myString, null/*dummy*/);

A good wrapper would be

public static void DebugFormat(string fmt, params object[] p)
{
     Debug.WriteLine(fmt, p); // this will select the right overload 
                              // ... due to typeof(p)==object[]
}

// ...
DebugFormat("Put text here: {0}", myString, null/*dummy*/);
int myNumber = 1;
DebugFormat("Put number here: {0}", myNumber);
like image 186
sehe Avatar answered Sep 22 '22 12:09

sehe


You are inadvertently calling a different overload:

http://msdn.microsoft.com/en-us/library/1w33ay0x.aspx

For the desired behavior, you can use string concatenation:

Debug.WriteLine("Put text here: " + myString);

or a call to String.Format:

Debug.WriteLine(String.Format ("Put text here: {0}", myString));
like image 42
sq33G Avatar answered Sep 19 '22 12:09

sq33G


Try:

Debug.WriteLine(string.Format("Put number here: {0}", 1));

Also, make sure that your Output option (drop down menu) in Visual Studio is set to Debug... It is not on by default in many cases.

like image 33
Hotrodmonkey Avatar answered Sep 19 '22 12:09

Hotrodmonkey