Introduction to precompiled directives in C #

Source: Internet
Author: User

Original: Introduction to Precompiled directives in C #

1, #define和 #undef

Usage:

#define DEBUG

#undef DEBUG

#define告诉编译器, I define a symbol for debug, which resembles a variable, but it has no specific value and can be seen as a symbol. #undef就是删除这个符号的定义. If the symbol debug has not been defined, then #undef does not work, otherwise the # define does not work. Both must be placed before the source code. Look at the order of the Code in the order of the two:

#define DEBUG

#undef DEBUG

In this case, debug is undefined, and if the two are in a different order, the compiler will assume that debug is defined

2, #if, #elif, #else, #endif

This tells the compiler to compile the code of the Process control. Consider the following code:

#if DEBUG
Console.Write ("Debug");
#elif RELEASE
Console.Write ("Realse");
#else
Console.Write ("other");
#endif

The above code means that if debug is defined, output debug, define release, output Realse, otherwise output other. What if debug and Realse are defined? You can try it yourself, please.

3. #warning, #error

These two designations tell the compiler whether to give a warning or an error message. In addition to the error message, compilation stops

Refer to the following code

#if debug------) This default is # define if the current state of debug is checked
#warning "It's ddbug state now."
#elif release------) If the release status is selected, this is not the default, I do not know whether the test is correct
#warning "It's Realse state now."
#else
#error "and clear what state"


#endif

4, #region and #endregion

This two is used to form blocks of code.

5, #line (the use of this designation is not very clear, refer to MSDN)

This instruction can be changed to jian the compiler to display the warning and error messages in the file name and line number information, using #line default to return the line number to the default line number.

The following example shows how to report the two warnings associated with a line number. The #line 200 instruction forces the line number to 200 (although the default value is #7). The result of another line (#9) as the default #line instruction is followed by the usual sequence.
//Preprocessor_line.cs
public class MyClass2
{
   public static void Main ()
   { br>      #line
      int i;  //CS0168 on line 200< br>      #line default
      char c;  //CS0168 on Line 9
  }
}

Example 2
The following example shows how the debugger ignores hidden rows in code. When you run 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 step through the code, you will see that the debugger ignores hidden rows. Also note that the debugger ignores the breakpoint even if it is set on a hidden line.
Preprocessor_linehidden.cs
Using System;
Class MyClass
{
public static void Main ()
{
Console.WriteLine ("Normal line #1."); Set a break point here.
#line hidden
Console.WriteLine ("Hidden line.");
#line default
Console.WriteLine ("Normal line #2.");
}
}

Introduction to precompiled directives in C #

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.