SoFunction
Updated on 2025-03-06

How to write DEBUG output in standardized C#

Often in code, you need to use DEBUG to output something weird for testing. However, there is only one window to output. If there is a funny guy constantly outputting, the output window will not be able to see its own content.

(() =>
        {
          while (true)
          {
            ("I'm a funny guy");
          }
        });
        ("Helpful information");

So Funbi likes his test code because he needs to keep seeing the output window talking about himself, but a normal programmer only looks at useful things, so he will take out a knife to kill Funbi. So how can funny prevent himself from being killed? He told the programmer to comment out the code

(() =>
        {
          while (true)
          {
            //("I am funny");          }
        });
        ("Helpful information");

But the programmer commented, it didn't work, because funny man wrote such code in many places

(() =>
        {
          while (true)
          {
            //("I am funny");          }
        });

        ("Helpful information");

        (() =>
        {
          while (true)
          {
            ("I'm a funny guy 1");
          }
        });

Finally, the programmer commented all the code and could run it, but Funbi got the programmer's code and found that there was no output. At this time, he was unhappy again, so Funbi removed all the comments and prepared to comment the code when uploading the code. But accidentally, there was a place where there was no comment, so I was beaten by the programmer.

So how do you need a funny guy to prevent being beaten by programmers? Next I will tell you a method, using #if

Usually you will see this writing method in the code

(() =>
        {
          while (true)
          {
# if DEBUG
            ("I'm a funny guy");
# endif
          }
        });

But this is wrong, because DEBUG is only used in code that all developers need to use in testing. If you use it yourself, you usually define it yourself. So how to define it? You need to write the following code at the beginning of the file

# define DEBUG_Uncomment shows Lin Dexi debuggingusing System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

The format defined is DEBUG_Uncomment and add function afterwards

The code can be modified to

(() =>
        {
          while (true)
          {
# if DEBUG_Uncomment shows Lin Dexi debugging            ("I'm a funny guy");
# endif
          }
        });

So I use this method for everything I use myself. I just need to comment out my definition and remove all my code. This way, there is no need to comment one by one, and this code is usually removed when uploading the code.

However, this method has a disadvantage. It can only work in one file. If there are multiple files that require writing their own code, then you still need to add this sentence to multiple files. If you don’t remember to comment all files when uploading the code, you will still be beaten.

Summarize

The above is the editor’s introduction to how to write DEBUG output in standardized by C#. I hope it will be helpful to everyone!