C # pre-compilation directives for required knowledge

Source: Internet
Author: User

Pre-compiling, also known as preprocessing, is the replacement of code text. Processing # At the beginning of the instructions, such as copy #include contained in the file code, #define宏定义的替换, conditional compilation, etc., is the preparatory work for the compilation of the stage, the main processing # start of the precompiled instructions.

The following is an introduction to the precompilation directives in C #:

1. #define和 #undef

Usage:

#define DEBUG

#undef DEBUG

#define告诉编译器, I define a debug symbol that resembles a variable, but it has no specific value and can look at it as a symbol. #undef就是删除这个符号的定义. If the symbol debug is not defined, then #undef does not work, otherwise #define does not work. Both must be placed before the source code. The order of the two looks at the order of the Code:

#define DEBUG

#undef DEBUG

In this case, debug is undefined, and if the two are in a different order, the compiler will assume that debug is defined

2. #if, #elif, #else, #endif

This tells the compiler to compile the code in Process Control. Consider the following code:

#if DEBUG
  Console.Write("debug");
#elif RELEASE
  Console.Write("realse");
#else
  Console.Write("other");
#endif

The above code means that if debug is defined, the output is debug, the release is defined, the output is realse, otherwise the output is other. What happens if debug and realse are defined? You can try it yourself.

3. #warning, #error

These two designations let you tell the compiler whether to give a warning or an error message. The compilation will stop except for the error message.

Refer to the following code:

#if DEBUG           ------〉该DEBUG如果选中当前状态为DEBUG,则这个默认为#define
  #warning “现在是Ddbug状态”
#elif RELEASE         ------〉如果选中release状态的话,这个确没有默认,不知道我试验的对不对
  #warning “现在是Realse状态”
#else
  #error“并清楚什么状态”
#endif

4. #region and #endregion

This two is used to make up a block of code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.