Can preprocessor directive lines be used in C# like in C?
Preprocessor directive lines like
#define
#include
#ifdef and #endif
#if and #elif 开发者_C百科
can be used in C, but not in C#.
Is there an alternative to these?
Mainly #define
.
C# has a crude preprocessor which allows you to define symbols and test for their existence.
Unless you are programming in C, for the love of Knuth, please do not ever use C-like preprocessors.
C# Preprocessor Directives (MSDN)
However, the C# compiler does not have a separate preprocessor like C, so things may be slightly different than what you are used to. The C# team wrote a blog entry about why C# doesn't support #define macros that may be interesting reading.
In C#, #define is only used for defining symbols that can be tested using the other preprocessor directives, #if and #endif. Therefore, you cannot use #define the same way as in C to define a constant value. To do this, you should use either a constant or enum. A constant must be inside of a class, but enums can be either inside or outside of classes.
Yes, C# has a preprocessor. It is somewhat limited compared to C++.
The following directives exist.
#if
#else
#elif
#endif
#define
#undef
#warning
#error
#line
#region
#endregion
#pragma
#pragma warning
#pragma checksum
精彩评论