What is the usage of #if DEBUG pre-processor directive in C#? When must we use this?

前端 未结 5 914
梦谈多话
梦谈多话 2021-01-11 10:55

What is the usage of #if DEBUG pre-processor directive in C#? When must we use this?

相关标签:
5条回答
  • 2021-01-11 11:09

    You might feel more comfortable with the Conditional attribute, which can be used to exclude entire methods, without needing to complicate the source with conditionals on the calling side:

    [Conditional("DEBUG")]
    void Foo()
    {
    }
    

    Foo() can be safely called in both debug and release - however in release mode, it would be a no-op.

    0 讨论(0)
  • 2021-01-11 11:10

    In Debug mode:

    #if DEBUG
                System.Console.WriteLine("Debug version");
    #endif
                System.Console.WriteLine("Output");
    

    Output as

    Debug version
    Output
    

    In Release mode:

    #if DEBUG
                System.Console.WriteLine("Debug version");
    #endif
                System.Console.WriteLine("Output");
    

    Output as

       Output
    

    read this: #if (C# Reference)

    Usage: If you have a set of values to be tested in the debug mode and not in the release mode you can use #if DEBUG

    0 讨论(0)
  • 2021-01-11 11:10

    When compiling you can set Compiler flags which you can use to put code into those directives. That code will not be compiled and never ends up in the final assembly output. DEBUG is one of the predefined ones, but you can use your own.

    As an example of usage: In one of the current developments we use a compiler flag to state whether to use A login mask to log in a user, or whether login should occur automatically with the current principal. The second mode is only for the developers to e.g. debug quicker without having to log in.

    Another example: In some mono code you will see flags. In this case code may be compiled differently when e.g. targetting a different framework, as it uses classes that may not exist in earlier releases.

    Related to this is the Conditional-Attribute with which you can mark a method. If said flag isn't set, calls to the method will not be performed. The method still ends up in the IL, but calls will be removed.

    Check for example following code:

    var mthods = typeof (Debug).GetMethods().Where(mi => mi.Name.Equals("WriteLine")).ToList();
    var attribs = mthods[0].GetCustomAttributes(true);
    

    You will notice that the Debug.WriteLine method has the Conditional attribute applied to it: Calls to it will be removed when you compile WITHOUT the DEBUG compiler flag.

    0 讨论(0)
  • 2021-01-11 11:13

    You don't have to use it at all. The purpose of it is to have sections of code that only get compiled in debug mode. e.g. you might have some code that enabled a master user, that could pretend to be any other user in the system for testing and debug pruposes. You would not want that user enabled in the release code for security reasons so you waould wrap the relevent sections of code in #if DEBUG and they would be excluded from release code.

    0 讨论(0)
  • 2021-01-11 11:34

    it also comes in handy when testing windows services. You can put in an #if DEBUG to start your process manually, so you don't have to install the service and attach to it to debug.

    0 讨论(0)
提交回复
热议问题