[C #] C # tutorial -- Preprocessor commands

Source: Internet
Author: User

Previous ArticleArticleRecords the knowledge of flow control statements in C #. This section describes the pre-processor commands in C, C # Preprocessor commands allow the compiler to do a lot of things that the C # basic syntax cannot do. I have only read this before and have never actually used it. This is just the case of this tutorial, this article records 2 things that I think are useful to me.

The following is what we will introduce:

1. # If, debug, And. Net versions

2. compiler warning and disabling warning

3. Other commonly used pre-compiler commands

 

I. # If, debug, And. Net versions

In whatever circumstances, the IF condition judgment statement is always the most common, while the # If statement of the compiler is more useful. For example, the # If statement can be used to judge the current one.. Net version, whether the current status is release.

For exampleCodeIn the debug status, if you visit the forum page,All SQL stored procedures, stored procedure parameter values, and execution time executed on the page are displayed at the bottom of the page, which greatly facilitates routine debugging and optimization.Such:

The corresponding # If Preprocessor command is also very convenient to use, the following code:

# Define debug # If debug // dosomething # endif

The above Code uses # define to define a symbol in advance, and then uses # If to determine whether it is true, # If syntax and VB. net is a bit similar. It must end with # endif. Otherwise, an error will occur.

You can also use # If to determine the current C # version, as shown in the following code:

 
# If csharp2using system. Collections. Generic; # endif

The code above judges that if the current. Net version is C #2.0, the system. Collections. Generic namespace will be introduced.ProgramIt is useful during development.

In the same Preprocessor commands, the command # else, # Elif is the same as the if syntax in C #. It will be better to use it in combination. The following example of msdn is referenced:

// Preprocessor_if.cs # define debug # define vc_v7using system; public class myclass {static void main () {# if (debug &&! Vc_v7) console. writeline ("Debug is defined"); # Elif (! Debug & vc_v7) console. writeline ("vc_v7 is defined"); # Elif (debug & vc_v7) console. writeline ("Debug and vc_v7 are defined"); # else console. writeline ("Debug and vc_v7 are not defined"); # endif }}

The above code outputs the corresponding string based on the version and status not used by the compiler.

 

Ii. compiler warning and disabling warning

In the process of daily development, we often compile the code, and a lot of information appears during the compilation process, A lot of useless warning information will always be prompted in a simple process to interfere with some major warnings. This can also be prevented by disabling the pre-processor command, common Warnings are as follows (cs1591, cs1573, etc ):

For example, cs1591 is generated because no corresponding XML comment is specified when the DLL is generated for the Assembly at the same time, we can ignore or close this common warning because it has no effect on the program. In this case, we can use # pragma warning to close or enable it. The following code:

 
# Pragma warning disable/restore 1591

Disable/restore indicates the disabled and enable warnings respectively, and the following number indicates the Warning number. The warning number does not need to start with "CS.

This code can only be written in the class file to be disabled or enabled, so that the corresponding 1591 warning will not appear.

However, note that # pragma warning only applies to a single file. To disable warnings for multiple files, use the/nowarn instruction of the compiler.For details, see msdn: click I to enter

PS: # pragma is a separate command, and warning is only one option. # The role of Pragma is:It is used to provide special instructions to the editor to explain how to compile a file containing a complex note.

 

 

Iii. Other Common Pre-compiler commands

There are still many pre-compiler commands, so I will not explain them in detail. Below I will just list and give a brief introduction. For details, refer to msdn: Click here to go

1. # UNDEF, corresponding to # define, undefined symbol

Usage:

# UNDEF debug

 

2. # Warning, warning, used for custom warning

Usage:

# Warning "this is a warning"

 

3. # line, line number. Change the line number of the compiler error or warning alarm.

Usage:

12 (actual row number) # line 50 "program. CS" // change the row number to 50

# Line default // restore the default line number

 

4. # error, which is used to generate an error at a specific location

Usage:

# Error this is an error

 

5. # region # endregion: collapse the code block

Usage:

# Region

Int A = 0;

# Endregion

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.