C # Preprocessor commands,

Source: Internet
Author: User

C # Preprocessor commands,

 

Preprocessor commands in C #

Directory

1. # define and # undef

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

3. # warning and # error

4. # region and # endregion

5. # line

6. # pragma

 

C # contains many commands named "Preprocessor commands. These commands will never be converted into executable code commands, but will affect all aspects of the compilation process.

For example, you can use a Preprocessor command to prohibit the compiler from compiling a part of the code. If you plan to release two versions of code, the basic version and the enterprise version with more features, you can use these pre-processor commands. When compiling a basic software version, you can use a Preprocessor command to prohibit the compiler from compiling Code related to additional functions.

In addition, you can use Preprocessor commands when writing code that provides debugging information. In fact, when selling software, you generally do not want to compile this part of code.

The Preprocessor commands start #.

 

C ++ developers should know that pre-processor commands in c and C ++ are very important. However, in C #, there are not so many pre-processor commands, they are rarely used. C # provides other mechanisms to implement many C ++ commands, such as custom features. Note that C # does not have an independent pre-processor like C ++. The so-called pre-processor commands are actually processed by the compiler.

Despite this, C # retains some Preprocessor command names, because these commands may be considered as preprocessors.

The following describes the functions of Preprocessor commands.

 

 

1. # define and # undef

# Define: # define DEBUG

It tells the compiler that there is a given name symbol. In this example, It is DEBUG. This is similar to declaring a variable, but this variable does not have a real value, but it only exists.

This symbol is not part of the actual code, but exists only when the compiler compiles the code. It does not make any sense in C # code.

 

# Undef is the opposite -- it deletes the definition of the symbol: # undef DEBUG

If the symbol does not exist, # undef does not work. Similarly, if the symbol already exists, # define does not work. You must place the # define and # undef commands at the beginning of the C # source file before declaring the code of any object to be compiled.

# Define is useless, but it is very powerful when used with other pre-processor commands (especially # if.

 

Note the General C # syntax changes. The Preprocessor command does not end with a semicolon. Generally, there is only one command on one line. This is because for Preprocessor commands, C # does not require commands to be separated by semicolons. If it encounters a Preprocessor command, it will assume that the next command is on the next line.

 

 

 

 

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

These commands tell the compiler whether to compile a code block. Consider the following method:

1 int DoSomeWork(double x)2 {3   // do something4   #if DEBUG5   Console.WriteLine("x is " + x);6   #endif7 }

This code is compiled as usual, but the Console. WriteLine command is included in the # if clause.

This line of code is executed only after the # define command defines the symbol DEBUG.

When the compiler encounters the # if statement, it first checks whether the related symbols exist. if the symbols exist, it compiles the code in the # if clause. Otherwise, the compiler ignores all the code until it encounters a matching # endif instruction.

Generally, DEBUG is defined during debugging, and debugging-related code is put in the # if clause. After debugging, comment out the # define statement. All the debugging code will miraculously disappear and the executable file will become smaller, the end user will not be confused by the debugging information (obviously, more tests are required to ensure that the code can also work without defining DEBUG ).

This technique is very common in C and C ++ programming and is called conditional compilation ).

 

# Elif (= else if) and # else commands can be used in the # if block, which has a very intuitive meaning. It can also be nested # if block:

#define ENTERPRISE#define W2K// further on in the file#if ENTERPRISE// do something#if W2K// some code that is only relevant to enterprise// edition running on W2K#endif#elif PROFESSIONAL// do something else#else// code for the leaner version#endif

 

Unlike in C ++, using # if is not the only way to compile code with conditions. C # also provides another mechanism through the Conditional feature.

 

# If and # elif also support a set of logical operators "!" , "=", "! = "And" | ". If a symbol exists, it is considered true; otherwise, it is false. For example:

1 #if W2K && (ENTERPRISE==false) // if W2K is defined but ENTERPRISE isn't

 

 

 

 

3. # warning and # error

The other two very useful pre-processor commands are # warning and # error. When the compiler encounters them, they will generate warnings or errors respectively. If the compiler encounters the # warning command, the text following the # warning command is displayed to the user, and the compilation continues. If the compiler encounters the # error command, it will display the subsequent text to the user as a compilation error message and immediately exit the compilation without generating the IL code.

Use these two commands to check whether the # define statement has done something wrong. Use the # warning statement to remind you to execute an operation:

1 #if DEBUG && RELEASE2 #error "You've defined DEBUG and RELEASE simultaneously!"3 #endif4 #warning "Don't forget to remove this line before the boss tests the code!"5 Console.WriteLine("*I hate this job.*");

 

 

 

 

4. # region and # endregion

The # region and # endregion commands are used to mark a piece of code as a block with a given name, as shown below.

1 #region Member Field Declarations2 int x;3 double d;4 Currency balance;5 #endregion

This seems useless and does not affect the compilation process. The advantage of these commands is that they can be recognized by some editors, including the Visual Studio. NET editor. These editors can use these commands to make code better laid out on the screen.

 

 

 

 

 

5. # line

# The line command can be used to change the file name and line number information displayed by the compiler in the warning and error messages. This command is rarely used.

If you want to use some software packages to change the entered code before sending the code to the compiler, you can use this command, this means that the row number or file name reported by the compiler does not match the row number in the file or the edited file name.

# The line command can be used to restore this matching. You can also use the syntax # line default to restore the row number to the default row number:

1 #line 164 "Core.cs" // We happen to know this is line 164 in the file2 // Core.cs, before the intermediate3 // package mangles it.4 // later on5 #line default // restores default line numbering

 

 

 

 

6. # pragma

# The pragma command can suppress or restore specified compilation warnings. Unlike the command line option, the # pragma command can be executed at the class or method level to control the content of the alarm suppression and the suppression time in more detail.

In the following example, the "field not used" Warning is disabled, and the warning is restored after MyClass class is compiled.

1 #pragma warning disable 1692 public class MyClass3 {4   int neverUsedField;5 }6 #pragma warning restore 169

 

 

Thank you for watching. Thank you.

 

 

 

 

References: C # advanced programming version 8th

 

Related Article

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.