When to use preprocessor directives in .net?

后端 未结 7 1668
情话喂你
情话喂你 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.

提交回复
热议问题