C # Preprocessor commands, do you create them ??? (● & #39; Middle & #39; ● ),

Source: Internet
Author: User

C # Preprocessor commands, do you create them ??? (● 'Authorization' ● ),
What is a c # preprocessing command ??The compiler command embedded in the C # source code.C # What are Preprocessor commands ??

These are pre-processor commands.

 

Next let's talk one by one (● 'authorization' ●)

1. # if, # elif, # else, endif

The first step of c # compilation is preprocessing. In this step, it is processed according to the commands starting with # in the source program (preprocessing commands. For example:
First, preprocessing first scans# DefineDebug1, the pre-processor knows that you have defined Debug1
Then, preprocessing is followed by Scanning# IfDebug1. Because Debug1 has been defined, this condition is true.
System. Console. WriteLine ("I'm Debug1 (● 'authorization' ●)") "extract" and hand it over to subsequent compilation.
Pre-processor scans# ElifAfter Debug2, Console. WriteLine ("Hello, I am Debug2") is ignored ");.
Scanned# ElseSystem. Console. WriteLine ("nothing, so output me") will be ignored. This statement will not be handed over to the compiler for compilation.
Finally:
After preprocessing, only System. console. writeLine ("I'm Debug1 (● 'authorization' ●)"); the statement is handed over to the compiler for code generation. The other two statements are not handed over to the compiler and are ignored, therefore, no code is generated.

Note:

Combined Use# IfAnd# Else, # elif, # endif, # defineAnd# UndefCommand to include or exclude code based on whether one or more symbols exist. This can be useful when compiling the code of the debug version or compiling specific configurations.

To# IfThe conditional command starting with the command must be used.# EndifThe command is explicitly terminated.

 

2. # define, # undef command

Use# DefineYou can define a symbol and pass it as an expression# IfCommand to make the expression calculatedTrue.

For example:

Note:

You can define symbols, but cannot assign values to symbols.

# DefineThe command must appear in the file before using any command that is not an instruction. # Define Debug1 must be written before all using

Available# UndefTo cancel the definition.

Use# DefineThe scope of the created symbol is the file in which the symbol is defined.

 

# UndefThis allows you to cancel the definition of a symbol so that the symbol can be used# IfThe expression in the command, so that the expression calculation result isFalse.

For example:

 

3. # generate a warning for warning, # generate an error for error

After the # warning command is included, the compiler sends a warning.

After the # error command is included, the compiler will take the initiative to make an error.

For example:

 

4. # pragma, # pragma warning

Warnings can indicate possible problems in the code, so they are useful. However, some warnings can be ignored reasonably, so it is necessary to close them.

Use preprocessing commands # disable pragma # warning commands

Example: # pragma warning disable 1030

Use the preprocessing command # pragma to restore the warning

Re-enable warning is still using the # pragma command, just add the restore option after warning

Example: # pragma warning restore 1030

The preceding two commands can enclose a specific code block, provided that the warning is not applicable to this code block.

For example:

 

5. # line (actually, it is just changing the code line number)

# LineThis allows you to modify the row number of the compiler 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 200The Command forces the row number to be 200 (although the default value is #7 ). The other line (#9) is used as the default# LineThe result of the command follows the normal sequence.

(The following remarks are taken from: https://msdn.microsoft.com/zh-cn/library/34dk387t (v = vs.80). aspx)

Note:

# LineThe command may be used by an automatic intermediate step 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 then use# LineSimulate the original row number.

# Line hiddenThe command hides several consecutive lines from the debugger, so that when the developer passes the code one by one,# Line hiddenAnd next# LineCommand (assuming it is not another# Line hiddenCommand. 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.

# Line hiddenThe 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 filenameCommand to specify 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.

Source code files can have# LineAny number of the command.

 

6. # region, # endregion

# RegionAnd# EndregionIs to fold or expand the code block,# RegionWhen the code is folded, we can see that# RegionDescription.

For example:

Remarks # RegionThe block must start # EndregionCommand termination.

# RegionThe block cannot match# IfBlocks overlap. However# RegionBlocks are nested in# IfBlock, or# IfBlocks are nested in# RegionBlock.

 

(The following section is taken from: https://msdn.microsoft.com/zh-cn/library/ms173226 (v = vs.80). aspx)

7.# 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 checksumProvides validation 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 encounter# Pragma checksumCommand, which calculates the checksum and then writes the calculated value to the PDB file.

 

Finally:

C # pre-processing command, which can be used to distinguish the files generated by compilation. Using a Preprocessor command can prohibit the compiler from compiling a part of the code. When you plan to release multiple different versions of the code, you can use the pre-processor command. When Compiling different versions of software, using Preprocessor commands can also prohibit the compiler from compiling Code related to additional functions. In addition, you can also use pre-processor commands when writing code that provides debugging information.

 

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.