Search code examples
c#scopec-preprocessorpreprocessor-directive

c# Pre-processor directive scope


I'm looking to use:

#define

and

#if

to allow me to simulate potentially absent hardware during unit tests. What are the rules for using the #define statements?

i.e. what is its default scope? can I change the scope of the directive?


Solution

  • As Chris said, the scope of #define is just the file. (It's worth noting that this isn't the same as "the class" - if you have a partial type, it may consist of two files, one of which has symbol defined and one of which doesn't!

    You can also define a symbol project-wide, but that's done with project properties or a compiler switch rather than being specified in source code.