简体   繁体   中英

Can preprocessor directive lines be used in C# like in C?

Preprocessor directive lines like

#define
#include
#ifdef and #endif
#if and #elif 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM