Preprocessing instruction and c preprocessing instruction

Source: Internet
Author: User

Preprocessing instruction and c preprocessing instruction
C # basic concepts of preprocessing commands

Although it is the same as the name of the term "preprocessing instruction" in C and C ++, C # does not actually have a separate preprocessing step, that is, there is no pre-processing compiler in environments such as.

The Preprocessing command can be used to replace the information in the source file. It must start with the # (Sharp) character and the name of the preprocessing command. For example:

#define MAX

We are used to set all names of preprocessing commands to uppercase. Unlike C and C ++, the name of the preprocessing instruction in C # is not followed by a number. The macro functions supported by C \ C ++ are not supported in C.

You can also use # region to set a region, or use Visual Studio to collapse the code. A string with the region name added after # region can be used to annotate the region.

# Region C # preprocessing command # endregion
Five pre-processing commands

1. # undef

We used # define above, but there are still # undef. So what does it do?

# Define MAXstatic void Main (string [] args) {# if (MAX) Console. WriteLine ("MAX has been defined. "); # Endif}

If we Debug the program, we will see the following in the console:

MAX has been defined.

If we Release the program, the results will be the same, but if we add the following at the beginning of the Code:

#undef MAX

Then, the Debug console will not receive any message.

2. # if, # else, # elif

If you want the console to display a situation where MAX is not defined, use # else To match # if.

# If (MAX) Console. WriteLine ("MAX has been defined. "); # Else Console. WriteLine (" MAX is not defined yet. "); # Endif

Maybe you also saw # elif when typing # else. This is the corresponding else if. Their logic is the same as that of the general if set.

# Define MAX # undef MAX # define MINstatic void Main (string [] args) {# if (MAX) Console. WriteLine ("MAX has been defined. "); # Elif (MIN) Console. WriteLine (" MIN has been defined. "); # Else Console. WriteLine (" MAX is not defined yet. "); # Endif Console. ReadLine ();}

3. # warning, # error

These two preprocessing commands are used to throw exceptions and errors.

# Warning: This is a forced exception.

# Error: This is a force-throw error.

4. # line

We can also use # line to set the number of lines in the following # error line code to 10000, and set the file name to "error".

Static void Main (string [] args) {# line 10000 "error" # error is a force-throw error. }

If we add such a line of code, the row number and file name will be restored to the default one.

# Line 10000 "Error" # error is a force-throw Error. # Line default # warning is a forced warning.

5. # pragma

Add this line of code before the Program class, which will cause the CS3021 error.

 [CLSCompliant(false)]    

Then we add the following line of code before the above line of code to disable warning 3021. Then Debug again and you will find that the original error is no longer there.

#pragma warning disable 3021    

If you want to restore the CS3021 error in the following text, use restore.

#pragma warning restore 3021    

The complete code is as follows.

#pragma warning disable 3021      [CLSCompliant(false)]      class Program      {                                   static void Main(string[] args)              {}  }#pragma warning restore 3021      [CLSCompliant(false)]      public class OtherProgram      {                                  public static void OtherMain()              {}  }

This pre-processing command is actually very complicated and has been introduced on major encyclopedias. The following is from Wikipedia. Portal: pragma once.

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.