When to use preprocessor directives in .net?

后端 未结 7 1632
情话喂你
情话喂你 2020-12-15 18:01

I think this is a simple question so I assume I\'m missing something obvious. I don\'t really ever use preprocessor directives but I was looking at someone\'s code which di

相关标签:
7条回答
  • 2020-12-15 18:05

    Generally, the optional/conditional compilation symbols will be provided by the build script. It is pretty rare to see #define, except for very debug code (if you see what I mean).

    Re using a variable; I often use such conditions to handle code that must run on different runtimes (mono, cf, silverlight, etc). A variable cannot suffice because the code cannot be compiled against the wrong platform (missing types/methods etc).

    In the example presented I would probably just have used Debug.WriteLine; since this is decorated with [Conditional("DEBUG")], all calls to it are automatically removed if DEBUG is not defined at build.

    0 讨论(0)
  • 2020-12-15 18:05

    I would like to give one example where I have used preprocessor directive in my project.

    My program creates lot of intermediate files on disk. I used #DEBUG directive to delete those files only if my project is in release mode, otherwise I keep those file so that we can view those intermediate files and determine whats happening inside.

    When my app is working on production server, I build project in release mode so those files are deleted after processing is complete.

    #if (DEBUG==false)
        deleteTempFiles()
    #endif
    
    0 讨论(0)
  • 2020-12-15 18:08

    I would actually recommend using the Conditional Attribute instead of inline #if statements.

    [Conditional("DEBUG")]
    private void DeleteTempProcessFiles()
    {
    }
    

    Not only is this cleaner and easier to read since you don't end up having #if, #else within your code. This style is less prone to errors either during normal code edits and well as logic flow errors.

    0 讨论(0)
  • 2020-12-15 18:13

    I've used it for a lot of things. Debug messages that I only want in debug builds; clean up temp files; include diagnostic functions or actions.

    0 讨论(0)
  • 2020-12-15 18:14

    in the example above why do they define DEBUG? I was under the impression that was set if you compile in debug v. release mode?

    Probably because it is example code. It is meant to demonstrate how #ifdef and friends work. I wouldn't expect you to define symbols like that in source files, unless it is for a quick test.

    looking at the other example which has "#define MYTEST" and then writes to the console dependent on if it 'defined', but how does this differ from just using a variable? What am I missing here?

    If MYTEST is not defined at compile time, the compiler will not actually emit the code between the #if and #endif blocks. Therefore the resultant IL will be smaller.

    Also, note that these are not preprocessor directives in C#.

    0 讨论(0)
  • 2020-12-15 18:15

    If you use variable all your code is compiled, when you use preprocessor directives only part of code included in executable/dll.

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