C # preprocessing scope

I'm looking for:

#define 

and

 #if 

to allow me to simulate potentially missing equipment during unit tests. What are the rules for using #define ?

i.e. What is its default area? can i change the scope of the directive?

+7
scope c # c-preprocessor preprocessor-directive
source share
5 answers

As Chris said, the #define area is just a file. (It is worth noting that this is not the same as the "class" - if you have a partial type, it can consist of two files, one of which has a specific character and one of which does not work!

You can also define a symbol throughout the project, but this is done using the project properties or the compiler instead of specifying in the source code.

+12
source share

From MSDN, its scope is a file

+3
source share

Although you could not follow the route of objects Mock, ala Mock.Rhinos ?

+1
source share

Yes, as Chris said, his area is the whole file. You can use a specific keyword anywhere in the file.

those.;

 #define something ... some code ... 

and in any method, class or namespace you can use it like:

 #if something ... some conditional code ... #else ... otherwise ... #endif 
+1
source share

The scope of the preprocessor directive begins when it is parsed from the source and stored until otherwise specified. If you want to limit the scope of the preprocessor directive, use the "undef" declaration, which it disables when you are done with it.

 #include <iostream> using namespace std ; int main() { #define someString "this is a string" cout<<someString<<endl; #undef someString // scope of someString ends here cout<<someString<<endl; //this causes a compile error return 0 ; } 
0
source share

All Articles