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

String.Format works fine with Debug.WriteLine if the variable does not have a string type:

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

Correct output with non-line

  • Enter the number here: 1

But if the variable is a string:

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

Invalid output with string

  • ShouldWork: Put the text here: {0}

Why?

+6
source share
3 answers

You get the wrong overload ...

You can get around this:

 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); 
+17
source

Try:

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

Also, make sure that the "Exit" (drop-down menu) option in Visual Studio is set to "Debug" ... In many cases, it is not enabled by default.

+2
source

You inadvertently cause another 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 calling String.Format:

 Debug.WriteLine(String.Format ("Put text here: {0}", myString)); 
+2
source

Source: https://habr.com/ru/post/901381/


All Articles