TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Debug.Indent and Unindent

This article describes the C# Debug type and its Indent and Unindent methods. It provides example programs.

Debug.Indent, Unindent. Debugging programs is repetitive.

The Debug type in System.Diagnostics makes using Debug.WriteLine easier. It provides the Indent and Unindent methods. These adjust the left-hand margin on the output. This makes it easier to read.

Example. First, this is a console program but you could use any type of .NET application. It includes the System.Diagnostics namespace. System.Diagnostics contains the Debug type and other types that inspect the operation of programs.

It calls Debug.WriteLine four times. After the first call it invokes Debug.Indent. And before the last call, it invokes Debug.Unindent. This means the middle two calls to Debug.WriteLine have an indent level (IndentLevel) of 1.

Note: Debug.IndentLevel is a property. Instead of using Indent and Unindent, you can assign to IndentLevel. This is sometimes clearer.

C# program that uses Indent and Unindent

using System.Diagnostics;

class Program
{
    static void Main()
    {
	// 1.
	Debug.WriteLine("One");

	// Indent and then unindent after writing.
	Debug.Indent();
	Debug.WriteLine("Two");
	Debug.WriteLine("Three");
	Debug.Unindent();

	// End.
	Debug.WriteLine("Four");

	// Sleep.
	System.Threading.Thread.Sleep(10000);

    }
}

Output

One
    Two
    Three
Four

Example 2. This example uses more Debug indentation properties. It accesses IndentSize, which is the number of spaces an indent has. It changes IndentSize to 2, and then directly sets IndentLevel to 1. This results in an indent of 2 spaces one time.

Tip: The total number of spaces that a Debug line will be indented is equal to the product of IndentSize and IndentLevel.

C# program that uses IndentSize

using System.Diagnostics;

class Program
{
    static void Main()
    {
	// Write IndentSize.
	Debug.WriteLine(Debug.IndentSize);

	// Change IndentSize.
	Debug.IndentSize = 2;
	Debug.IndentLevel = 1;
	Debug.WriteLine("Perls");

	// Sleep.
	System.Threading.Thread.Sleep(10000);
    }
}

Output

4
  Perls

Discussion. How you debug your programs is largely a personal choice. Programmers far more skilled than I am sometimes use Console.WriteLine (printf in C, cout in C++). Inspecting variables, as with Visual Studio, is a preferred method for some.

Opinion: Key, as a programmer, is an understanding of code behavior, its intent and meaning.

And: Programming is not about using the right tools or knowing what button to click.

Instead: It is a process of understanding, of knowing. It is less procedural than experiential and conceptual.

Summary. The Debug.Indent, Unindent, IndentSize and IndentLevel members simplify debug output. Knowing these methods and properties exist is the first key to using them. Depending on your environment, these constructs may be useful.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf