C # Preprocessor Directives

Source: Internet
Author: User

C # Preprocessor directives do not translate to executable code commands, but affect all aspects of the compilation process.

For example, when you plan to release two versions of the code. That is, the basic version and the Enterprise Edition with more versions, you can use the preprocessor directives,

When compiling the basic version, using preprocessor directives prevents the compiler from compiling code that is related to additional functionality.

You can also use preprocessor directives when writing code that provides debugging information

There are not many pre-processing instructions, so let's give a simple example, specific explanations and syntax, which can be found in the MSDN Help documentation

First, write the following code

usingSystem;namespaceconsoleapplication{classProgram {Static voidMain () {Console.WriteLine ("Debug State"); Console.WriteLine ("Tracking Status"); Console.WriteLine ("Running State"); Console.WriteLine ("Please enter a number:"); varnum =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}", num); }    }}

The post-operation effect is as follows:

If you use preprocessor directives, add instructions at the top of the code

#define DEBUG#define TRACE

Then, add the judgment at the location where you debug and track

#defineDEBUG#defineTRACEusingSystem;namespaceconsoleapplication{classProgram {Static voidMain () {#ifDEBUGConsole.WriteLine ("Debug State");#elifTRACEConsole.WriteLine ("Tracking Status");#elseConsole.WriteLine ("Running State"); Console.WriteLine ("Please enter a number:"); varnum =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}", num); #endif        }    }}

At this point, only the debug state of the code can be executed normally, if you use #undef to cancel the definition, then you can specify the code snippet to run, as

#defineDEBUG#defineTRACE#undefDEBUG#undefTRACEusingSystem;namespaceconsoleapplication{classProgram {Static voidMain () {#ifDEBUGConsole.WriteLine ("Debug State");#elifTRACEConsole.WriteLine ("Tracking Status");#elseConsole.WriteLine ("Running State"); Console.WriteLine ("Please enter a number:"); varnum =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}", num); #endif        }    }}

You can also display specified warning and error messages by #warning or #error directives

#warning   var num = convert.toint32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}"

Where warnings can be compiled, and errors cannot be compiled by normal, the effect is as follows:

You can get the number of the warning in the Output window

Suppress current warnings or errors by #pragma in code

#pragma warning Disable 1030

Finally, it is often used, using #region to achieve Code area segmentation

#region Here is the code area Console.WriteLine (" running state "); Console.WriteLine (" Please enter a number:"); #warning must add a defensive code here var num = convert.toint32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}", num); #endregion

The complete program code is as follows:

#defineDEBUG#defineTRACE#undefDEBUG#undefTRACE#pragmaWarning Disable 1030usingSystem;namespaceconsoleapplication{classProgram {Static voidMain () {#ifDEBUGConsole.WriteLine ("Debug State");#elifTRACEConsole.WriteLine ("Tracking Status");#else            #regionThis is the code area.Console.WriteLine ("Running State"); Console.WriteLine ("Please enter a number:");#warningThe defensive code must be added here.varnum =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}", num); #endregion#endif        }    }}

C # Preprocessor Directives

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.