C # Differences in if and # if

Source: Internet
Author: User

If the role of program flow control, will be directly compiled, executed.
#if是对编译器的指令, the function is to tell the compiler that some statement lines want to compile when the condition is met.

--------------------------------------------------------------------------------------------------------------- -----

#if allows you to start conditional directives, test one or more symbols to see if they evaluate to true. If they do evaluate to true, the compiler calculates all the code that is between the #if and the nearest #endif instruction. For example

Copy
#define DEBUG//. #if Debug    Console.WriteLine ("Debug Version"); #endif

You can use operators = = (equal),! = (unequal), && (and), and | | (or) to calculate multiple symbols. You can also group symbols and operators in parentheses.

Notes

Using #if and #else, #elif, #endif, #define, and #undef directives, you can include or exclude code based on conditions that consist of one or more symbols. This is most useful when compiling a debug version of code or compiling a specific configuration.

Conditional directives that begin with a #if instruction must be explicitly terminated with a #endif instruction.

#define allows you to define a symbol that evaluates to trueby using the symbol as an expression passed to the #if instruction.

You can also use the/define compiler option to define symbols. You can use #undef to cancel the definition of a symbol.

Symbols defined with /define or #define do not conflict with variables that have the same name. That is, the variable name should not be passed to the preprocessor directive, and symbols can only be computed with preprocessor directives.

The range of symbols created with #define is the file in which the symbol is defined.

Example Copy
Preprocessor_if.cs#define debug#define vc_v7using System;public class MyClass {    static void Main ()     {#if ( DEBUG &&! VC_V7)        Console.WriteLine ("DEBUG is defined"); #elif (! Debug && vc_v7)        Console.WriteLine ("VC_V7 is defined"), #elif (Debug && vc_v7)        Console.WriteLine ("Debug and VC_V7 are defined"), #else        Console.WriteLine ("Debug and VC_V7 is not defined"); #endif c6/>}}
Output
DEBUG and VC_V7 are defined

C # Differences in if and # if

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.