C # Preprocessor commands

Source: Internet
Author: User
Tags processing instruction

C # pre-processor commands are called during compilation.

Preprocessor directive tells C # Compiler which code to compile and how to handle specific errors and warnings.

The C # pre-processor command also tells the C # editor about the code organization.

1. Define the symbols and cancel the pre-processing instructions for the definition of the symbols # define and # UNDEF

The pre-processing commands start with # And are located at the beginning of the line. A space character can appear before them.

    #define DEBUG#define ISSAY

The preceding statement defines a pre-compiled symbol whose scope is the entire file. The statement defining the symbol must appear before all the code, otherwise, an exception occurs during compilation: the pre-processor symbol cannot be defined or undefined after the first mark of the file. We can also use # UNDEF to cancel the definition of a symbol. Let's look at an example.

# Define debug # UNDEF debug # define issay using system; namespace justdoit {class program {static void main (string [] ARGs) {# If debug console. write ("debug. "); # endif # If issay console. write ("hello. "); # else console. write ("You can say nothing. "); # endif console. readline () ;}}// output: Hello

From the code above, we can see that the first line defines a debug symbol, and the second line cancels the definition of this symbol, which is equivalent to no definition, so the console will not be executed when the program is running. write ("debug.. The third line defines the issay symbol, so the program outputs "hello". If we comment it out or delete it, the program outputs "you can say nothing". We can initially see that by defining pre-compiled symbols, the compiler can control the selective compilation of code. The above Code also contains symbols such as # If and # endif, which are conditional compilation instructions.

2. Conditional compilation commands

There are four Conditional compilation commands. Besides the # If, # else, And # endif commands we can see in the first example, there is also a # Elif command. We should have known each other about these commands. They are the same as the conditional statements we use when writing code. The conditional statements are used to control the program flow, these Conditional compilation commands are used to control the compiler to selectively compile code.

One # If statement can have 0 or more # Elif statements, or zero or one # else statement, but must include one # endif statement, otherwise, a syntax error occurs.

3. # region and # endregion

These two symbols are usually used a lot, that is, to fold some relevant Code together, which is very useful for us to compile long code in a file, we can organize a set of related Code together with # region and # endregion and add the explanatory text after # region. When this set of code is folded, we can see the description text behind # region.

C # Introduction to Preprocessor commands on msdn

Visual Studio 2013

Msdn

This section contains information about the following C # Preprocessor commands.

# If

# Else

# Elif

# Endif

# Define

# UNDEF

# Warning

# Error

# Line

# Region

# Endregion

# Pragma

# Pragma warning

# Pragma checksum

For more information and examples, see topics.

Although the compiler does not have a separate pre-processor, the processing instruction described in this section is like. They are used for help in Conditional compilation. Unlike C and C ++ commands, these commands cannot be used to create macros.

The Preprocessor command must be a unique command on the line.

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.