C # Tutorial C # Preprocessor directives

Source: Internet
Author: User

C # Preprocessor Directives

Preprocessor directives instruct the compiler to preprocess information before the actual compilation begins.

All preprocessor directives start with #. And on a single line, only whitespace characters can appear before preprocessor directives. Preprocessor directives are not statements, so they do not end with semicolons (;).

The C # compiler does not have a separate preprocessor, but the instruction is handled as if it had a separate preprocessor. In C #, preprocessor directives are used to work in conditional compilation. Unlike C and C + + directives, they are not used to create macros. A preprocessor directive must be the only instruction on that line.

C # Preprocessor Directives list

The following table lists the preprocessor directives available in C #:

Preprocessor directives

Describe

#define It is used to define a series of characters that become symbols.

#undef It is used to cancel the definition of a symbol.

#if It is used to test whether a symbol is true.

#else It is used to create compound conditional directives for use with #if.

#elif It is used to create compound conditional directives.

#endif Specifies the end of a conditional directive.

#line it allows you to modify the number of lines of the compiler and (optionally) output errors and warning file names.

#error it allows an error to be generated from the specified location in the code.

#warning it allows you to generate a one-level warning from a specified location in your code.

#region It allows you to specify an expandable or collapsed block of code when you use the outline attributes of Visual Studio code Editor.

#endregion it identifies the end of the #region block.

#define Preprocessor

Create symbolic constants #define preprocessor directives.

#define allows you to define a symbol so that the expression returns true by using the symbol as the expression passed to the #if instruction. Its syntax is as follows:

#define Symbol

The following procedure illustrates this:

#define PI using System;namespace preprocessordappl{   class program   {      static void Main (string[] args)      {         #if (PI)            Console.WriteLine ("PI is defined");         #else            Console.WriteLine ("PI is not defined");         #endif         Console.readkey ();}}}   

When the above code is compiled and executed, it produces the following results:

PI is defined

Conditional directives

You can use #if directives to create a conditional directive. Conditional directives are used to test whether a symbol is true. If true, the compiler executes the code between the #if and the next instruction.

Syntax for conditional directives:

#if symbol [operator symbol] ...

Where symbol is the symbolic name to be tested. You can also use True and false, or put a negative operator before the symbol.

The operator symbol is an operator used to evaluate symbols. The can operator can be one of the following operators:

= = (Equality)

! = (inequality)

&& (and)

|| (OR)

You can also use parentheses to group symbols and operators. Conditional directives are used to compile code when a debug version or a specified configuration is compiled. A conditional instruction that begins with a #if instruction must be signaled to terminate with a #endif instruction.

The following program demonstrates the use of conditional directives:

#define Debug#define vc_v10using System;public class testclass{public   static void Main ()   {      #if (DEBUG & &! VC_V10)         Console.WriteLine ("DEBUG is defined");      #elif (! DEBUG && vc_v10)         Console.WriteLine ("VC_V10 is defined");      #elif (Debug && vc_v10)         Console.WriteLine ("Debug and VC_V10 are defined");      #else         Console.WriteLine ("DEBUG and VC_V10 is not defined");      #endif      Console.readkey ();   }}

When the above code is compiled and executed, it produces the following results:

DEBUG and VC_V10 are defined


The above is the "C # Tutorial" C # preprocessor directive content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.