Sr.No. | Preprocessor Directive & Description |
---|---|
1 | #define It defines a sequence of characters, called symbol. |
2 | #undef It allows you to undefine a symbol. |
3 | #if It allows testing a symbol or symbols to see if they evaluate to true. |
4 | #else It allows to create a compound conditional directive, along with #if. |
5 | #elif It allows creating a compound conditional directive. |
6 | #endif Specifies the end of a conditional directive. |
7 | #line It lets you modify the compiler's line number and (optionally) the file name output for errors and warnings. |
8 | #error It allows generating an error from a specific location in your code. |
9 | #warning It allows generating a level one warning from a specific location in your code. |
10 | #region It lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. |
11 | #endregion It marks the end of a #region block. |
#define symbolThe following program 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 result −
PI is defined
#if symbol [operator symbol]...Where, symbol is the name of the symbol you want to test. You can also use true and false or prepend the symbol with the negation operator. The operator symbol is the operator used for evaluating the symbol. Operators could be either of the following −
#define DEBUG #define VC_V10 using 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 are not defined"); #endif Console.ReadKey(); } }When the above code is compiled and executed, it produces the following result −
DEBUG and VC_V10 are defined
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!