C # Preprocessor commands in the language-reprint + experience

Source: Internet
Author: User
Tags deprecated

Reference: http://blog.csdn.net/alwaysrun/archive/2009/08/20/4467270.aspx

Http://www.bianceng.cn/Programming/csharp/200910/11700.htm

C and C ++ have a class of statements called Preprocessor commands. C # also has such a set of Preprocessor commands. Three languages use the same method: A # symbol must be placed before a specific instruction. Unlike C and C ++ commands, these commands cannot be used to create macros. All Preprocessor commands must appear in their own command lines.

In C/C ++, there are actually two compilation rounds. Through the pre-processor command, you can rewrite the source code so that it corresponds to the specific structure and application before the second compilation. C #'s compilation process is not two phases. However, C # treats Preprocessor commands in a similar way as C/C ++.

 

C # pre-processor commands:

  • # If
  • # Else
  • # Elif
  • # Endif
  • # Define
  • # UNDEF
  • # Warning
  • # Error
  • # Line
  • # Region
  • # Endregion
  • # Pragma
  • # Pragma warning
  • # Pragma checksum

I. # If
# If enables you to start conditional commands and test one or more symbols to check whether they are calculated as true. If their calculation result is true, the compiler calculates all code located between # If and the nearest # endif command. Conditional commands starting with the # If command must be explicitly terminated using the # endif command. For example:

# Define debug
//...
# If debug
Console. writeline ("debug version ");
# Endif

You can use the = (equal) and! operators ),! = (Not equal), & (and), and | (OR) to calculate multiple symbols. You can also use parentheses to group symbols and operators.

Note:
Use the # If and # else, # Elif, # endif, # define, and # UNDEF commands to include or exclude code based on conditions consisting of one or more symbols. This is most useful when compiling and debugging code or compiling specific configurations.

Ii. # else
# Else allows you to create compound condition commands. Therefore, if the previous # If or (optional) # Elif command does not contain any expression true, the compiler calculates all the code between # else and # endif.

Note: # endif must be the next Preprocessor command of # Else.

Iii. # Elif

Allows you to create compound condition commands. If the calculation result of the # If and any # Elif (optional) command expression is not true, the # Elif expression is calculated. If the # Elif expression is calculated as true, the compiler calculates all code located between # Elif and the next conditional instruction.

Iii. # endif
# Endif specifies the end of a conditional instruction starting with # If.

Iv. # define
You can use # define to define a symbol and pass it as an expression to the # If command to make the expression calculate true. You can define symbols, but cannot assign values to symbols. For example:

# Define debug

A symbol can be used to specify the compiling conditions. You can use # If or # Elif to test the symbol. You can also use the conditional attribute to execute Conditional compilation.
You can also use the/define compiler option to define symbols. You can use # UNDEF to cancel the definition symbol. The range of the symbol created with # define is the file in which the symbol is defined.

Note: Unlike C/C ++, the # define Statement of C # only allows you to insert a label in the symbol table, but you cannot assign a value to any symbol. Different from C/C ++, C/C ++ allows # define Preprocessor commands to appear anywhere you need, before using any non-Preprocessor command, the # define command of C # must appear in a file. Labels defined with # define do not conflict with variables with the same name. That is to say, a variable name should not be passed to the Preprocessor command, and the symbol can only be evaluated through the pre-processor command. The scope of the label inserted to the symbol table is the file that defines it. You can use # UNDEF to cancel the definition of the symbol.

V. # UNDEF
# UNDEF allows you to cancel the definition of a symbol so that the expression is calculated as false by using the symbol as an expression in the # If command.

Vi. # warning
# Warning allows you to generate a level-1 warning from a specific position in the code. For example:
# Warning deprecated code in this method.

Note:
# Warning is usually used in conditional commands. You can also use # error (C # reference) to generate user-defined errors.
Example
// Preprocessor_warning.cs
// Cs1030 expected
# Define debug
Class mainclass
{
Static void main ()
{
# If debug
# Warning debug is defined
# Endif
}
}

VII. # error
# Error allows you to generate an error from a specific position in the code. For example:
# Error deprecated code in this method.

8. # Line
# Line allows you to modify the compiler's line number and (optional) Output of file names for errors and warnings. The following example shows how to report two warnings associated with the row number. # Line 200 Command forces the row number to be 200 (although the default value is #7 ). The result of another line (#9) as the default # Line command follows the normal sequence.

Class mainclass
{
Static void main ()
{
# Line 200
Inti; // cs0168 on line 200
# Line default
Char C; // cs0168 on line 9
}
}

Note:
# Line commands may be used by automatic intermediate steps during the generation process. For example, if the line is removed from the original source code file, but you still want the compiler to generate output based on the original line number in the file, you can remove the line and use # line to simulate the original line number. # Line hidden command hides several consecutive lines from the debugger, so that when a developer passes the code one by one, all lines between the # Line hidden and the next # Line command (assuming it is not another # Line hidden command) will be skipped. This option can also be used to enable ASP. NET to differentiate user-defined code from computer-generated code. Although ASP. NET is the main user of this function, more source generators may use it.
# The line hidden command does not affect the file name or row number in the error report. That is, if an error occurs in the hidden block, the compiler reports the current file name and the wrong row number.
# Line filename command specifies the file name that you want to appear in the compiler output. By default, the actual name of the source code file is used. The file name must be enclosed in double quotation marks. The source code file can have any number of the # Line command.

Example
The following example shows how the debugger ignores hidden rows in the code. When running this example, it displays three lines of text. However, when you set the breakpoint as shown in the example and press the F10 key to pass the code one by one, you will see that the debugger ignores hidden lines. Note that the debugger still ignores the breakpoint even if the breakpoint is set on the hidden row.
// Preprocessor_linehidden.cs
Using system;
Class mainclass
{
Static void main ()
{
Console. writeline ("normal line #1."); // set the breakpoint here
# Line hidden
Console. writeline ("hidden line .");
# Line default
Console. writeline ("normal line #2 .");
}
}

9. # Region
# Region allows you to specify a code block that can be expanded or collapsed when you use the outline display function of the Visual Studio code editor. For example:
# Region myclass Definition
Public class myclass
{
Static void main ()
{
}
}
# Endregion

Note:
# The region block must be terminated with the # endregion command.
# Region blocks cannot overlap with # If blocks. However, you can nest the # region block in the # If block or the # If block in the # region block.

10. # endregion
# Endregion mark # End of the region block.

XI. # pragma
# Pragma is used to provide special instructions to the editor to explain how to compile a file containing a complex note.
# Pragma-name pragma-arguments
Parameters
Pragma-name
Identifies the name of a hybrid injection.
Pragma-arguments
Special parameters.

12. # pragma warning
# Pragma warning can be used to enable or disable certain warnings.
# Pragma warning disable warning-list
# Pragma warning restore warning-list
Parameters
Warning-list
A comma-separated list of warning numbers. Enter only numbers, excluding the prefix "CS ".
If no warning number is specified, disable disables all warnings while restore enables all warnings.
Example
// Pragma_warning.cs
Using system;

# Pragma warning disable 414,302 1
[Clscompliant (false)]
Public Class C
{
Int I = 1;
Static void main ()
{
}
}
# Pragma warning restore 3021
[Clscompliant (false)] // cs3021
Public Class D
{
Int I = 1;
Public static void F ()
{
}
}

13th, # pragma checksum
It can be used to generate the checksum of source files to help debug ASP. NET pages.
# Pragma checksum "FILENAME" "{guid}" "checksum bytes"
Parameters
"FILENAME"
The name of the file to be changed or updated.
"{Guid }"
Guid ).
"Checksum_bytes"
A hexadecimal string that represents the bytes of the checksum. It must be an even hexadecimal number. The odd digit number causes a warning during compilation, so that the instruction is ignored.
Note:
The Visual Studio debugger uses a checksum to ensure that the source is always correct. The compiler calculates the checksum of the source file and sends the output to the PDB file. Finally, the debugger uses PDB to compare the checksum calculated for the source file.
This solution is not applicable to ASP. NET projects, because the generated source file is calculated instead of the. aspx file checksum. To solve this problem, # pragma checksum provides checksum support for ASP. NET pages.
When you create an ASP. NET project in Visual C #, the generated source file contains the checksum of The. aspx file (the source file is generated from this file. Then, the compiler writes this information to the PDB file.
If the compiler does not run the # pragma checksum command in this file, it calculates the checksum and then writes the calculated value to the PDB file.
Example
Class testclass
{
Static int main ()
{
# Pragma checksum "file. cs" "{3673e4ca-6098-4ec1-890f-8fceb2a794a2}" "{012345678ab}" // new checksum
}
}

 

 

 

 

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.