1/21
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Debug
A static class in the System.Diagnostics namespace used to emit debugging information and check program logic with assertions. Its methods are primarily intended for debug builds. Domain: C# → .NET → System.Diagnostics → Debugging Output & Assertions
Debug.Write("Loading…");
Writes debugging information without automatically adding a new line.
Debug.WriteLine("Program started");
Writes debugging information followed by a line terminator.
Debug.WriteLine(value);
Writes the value of an object to the debug listeners by using the object's ToString() representation.
Debug.WriteLine("Connected", "Network");
Writes a debugging message together with a category name that can help identify the type or source of the message.
Debug.WriteIf(condition, "Value is invalid");
Writes the specified debugging message only when the Boolean condition is true.
Debug.WriteLineIf(condition, "Value is invalid");
Writes the specified debugging message followed by a new line only when the Boolean condition is true.
Debug.Assert(age >= 0);
Checks that the specified condition is true and reports an assertion failure when the condition is false.
Debug.Assert(age >= 0, "Age cannot be negative");
Checks a condition and reports the supplied message if the assertion fails.
Debug.Assert(value != null, "Value must not be null", "Check the initialization logic.");
Checks a condition and provides additional diagnostic information when the assertion fails.
Debug.Fail("Unexpected code path reached");
Emits an assertion failure indicating that execution reached a location that should not normally be reached.
Debug.Fail("Invalid state", "State should have been initialized earlier.");
Emits an assertion failure with both a primary message and additional diagnostic details.
Debug.Indent();
Increases the indentation level applied to subsequent Debug output.
Debug.Unindent();
Decreases the indentation level applied to subsequent Debug output.
Debug.IndentLevel = 2;
Sets the current indentation level used for Debug output.
int level = Debug.IndentLevel;
Gets the current indentation level used by Debug output.
Debug.IndentSize = 4;
Sets the number of spaces represented by each indentation level.
int size = Debug.IndentSize;
Gets the number of spaces used for each Debug indentation level.
Debug.Flush();
Flushes buffered debugging output to the attached trace listeners.
Debug.Close();
Flushes output and closes the trace listeners associated with Debug.
Assertion
A debugging check that verifies an expected condition in program logic and reports a failure when that condition is false.
Debug vs Console.WriteLine
Debug.WriteLine() is intended for diagnostic/debugging output and is conditionally compiled for DEBUG builds, whereas Console.WriteLine() writes ordinary program output to the console during normal execution.