We help IT Professionals succeed at work.

How should I use debug.print in C#

william007
william007 asked
on
In VB, we can use debug.print to print the text in a console, but how do we do the similar thing in C#? The objective is to test the written class.
Comment
Watch Question

Top Expert 2008

Commented:
try

Debug.WriteLine
Top Expert 2008
Commented:
sorry should be
System.Diagnostics.Debug.WriteLine("test");

Author

Commented:
Hi, I choose Debug>Start Debugging, I can't see the word test appear in anyway...
Top Expert 2008

Commented:
maybe you have closed the immediate window

To display the Immediate window, choose Windows from the Debug menu and select Immediate.

View... Other Windows... Command Window
Type:
immed
at the > prompt and press enter.  The Immediate Window will magically appear.

private void button2_Click(object sender, EventArgs e)
{
StreamReader re = File.OpenText("c:\\MyFile.txt");
string input = null;
while ((input = re.ReadLine()) != null)
{
Debug.Print(input);
Debug.Write(input);
Debug.WriteLine(input);
}
re.Close();
}

Open in new window

This will work perfectly : Console.WriteLine( "test" );
Commented:
SolutionsCS - that will write to the Console, not the Debug Console. If you then run the code in a non-debug environment it will still print out, which isn't really what you want for debugging messages.

Debug messages appear in the Output window if you select "Show Output From Debug". If you want to see your debug messages without all the other cruft go to Tools->Options->Debugging->General and check "Redirect all Output Window text to the Intermediate Window" and, if the Intermediate Window is closed, follow xPert_Umer's instructions.

Author

Commented:
Thanks:)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.