Some C # pre-processor commands

Source: Internet
Author: User
Tags logical operators

Like C, C # has some pre-processor commands. For example, # If # end if, # define, and so on, these commands do not convert to some commands in the executable code, but play a role in the compilation process.
The following is a brief introduction:
1. # define and # UNDEF
# Define is used as follows:
# 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.
# The opposite of UNDEF -- Delete the definition of a symbol:
# UNDEF debug
If the symbol does not exist, # UNDEF does not work. Similarly, if the symbol already exists, # define does not work. The # define and # UNDEF commands must start with the C # source code 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:
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 # no longer requires the command to end with a semicolon. 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:

[CSHARP] int dosomework (Double X)
{
// Do something
# If debug
Console. writeline ("X is" + x );
# Endif
}
Int dosomework (Double X)
{
// Do something
# If debug
Console. writeline ("X is" + x );
# Endif
}
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 block. 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 ).
# 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:

[CSHARP]
# 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
# 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
# If and # Elif also support a set of logical operators! , = ,! = And |. If the symbol exists, it is considered true; otherwise, it is false.
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. Using these two commands, you can check whether the # define statement has done something wrong. Using the # warning statement reminds you of what you have done:

[CSHARP]
# If debug & release
# Error "You 've defined debug and release simultaneously! "
# Endif
 
# Warning "Don't forget to remove this line before the boss tests the code! "
Console. writeline ("* I hate this job *");
# If debug & release
# Error "You 've defined debug and release simultaneously! "
# Endif

# Warning "Don't forget to remove this line before the boss tests the code! "
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.

[CSHARP]
# Region
 
Member field declarationsint X;
Double D;
Currency balance;
 
# Endregion
# Region

Member field declarationsint X;
Double D;
Currency balance;

# 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 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 to the default row number:

[CSHARP]
# Line 164 "core. cs" // we happen to know this is line 164 in the file
// Core. CS, before the intermediate
// Package Mangles it.
 
// Later on
# Line 164 "core. cs" // we happen to know this is line 164 in the file
// Core. CS, before the intermediate
// Package Mangles it.

// Later on
6. # pragma
# The Pragma command can suppress or resume specified compilation warnings. Unlike the command line option, the # pragma command can be executed on classes or methods to control the content of the alarm suppression and the suppression time in a more precise manner. In the following example, the field warning is disabled, and the warning is restored after the myclass class is compiled.

[CSHARP]
# Pragma warning disable 169
Public class myclass
{
Int neverusedfield;
}
# Pragma warning restore 169

Some C # pre-processor commands

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.