GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C# - Preprocessor Directives
Selected Reading
C# - Preprocessor DirectivesThe preprocessor directives give instruction to the compiler to preprocess the information before actual compilation starts. All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not statements, so they do not end with a semicolon (;). C# compiler does not have a separate preprocessor; however, the directives are processed as if there was one. In C# the preprocessor directives are used to help in conditional compilation. Unlike C and C++ directives, they are not used to create macros. A preprocessor directive must be the only instruction on a line. Preprocessor Directives in C#The following list shows the preprocessor directives available in C# −
The #define PreprocessorThe #define preprocessor directive creates symbolic constants. #define lets you define a symbol such that, by using the symbol as the expression passed to the #if directive, the expression evaluates to true − Following is the syntax of #define preprocessor − #define symbol ExampleThe following Example demonstrates the use of #define preprocessor. Here, we use define the #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 Conditional DirectivesYou can use the #if directive to create a conditional directive. Conditional directives are useful for testing a symbol or symbols to check if they evaluate to true. If they do evaluate to true, the compiler evaluates all the code between the #if and the next directive. Following is the syntax of the conditional directives − #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 −
You can also group symbols and operators with parentheses. Conditional directives are used for compiling code for a debug build or when compiling for a specific configuration. A conditional directive beginning with a #if directive must explicitly be terminated with a #endif directive. ExampleThe following program demonstrates the use of conditional directives − #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 The #line PreprocessorThe Following is the syntax of #line number "filename" ExampleThe following example demonstrates the use of #line preprocessor − using System;
class Program {
static void Main() {
Console.WriteLine("Before #line directive");
#line 100 "MyFile.cs"
Console.WriteLine("This line number is 100 in MyFile.cs");
#line default
Console.WriteLine("After #line directive");
}
}
When the above code is compiled and executed, it produces the following result − Before #line directive This line number is 100 in MyFile.cs After #line directive The #error PreprocessorThe Following is the syntax of #error "Custom error message" ExampleThe following example demonstrates the use of #error preprocessor. The compilation will stop if #define DEBUG_MODE
using System;
class Program {
static void Main() {
#if DEBUG_MODE
#error "Debug mode is enabled. Disable it before compiling."
#endif
Console.WriteLine("Program running normally.");
}
}
When the above code is compiled, it produces the following error − Compilation failed: 1 error(s), 0 warnings main.cs(7,0): error CS1029: #error: '"Debug mode is enabled. Disable it before compiling."' The #warning PreprocessorThe Following is the syntax of #warning "Custom warning message" ExampleThe following example demonstrates the use of #warning preprocessor. A warning is displayed if #define BETA_VERSION
using System;
class Program {
static void Main() {
#if BETA_VERSION
#warning "This is a beta version. Use at your own risk."
#endif
Console.WriteLine("Program running normally.");
}
}
When the above code is compiled, it produces the following warning − Compilation succeeded - 1 warning(s) main.cs(7,0): warning CS1030: #warning: `"This is a beta version. Use at your own risk."' Program running normally. The #region and #endregion PreprocessorThe Following is the syntax of #region RegionName // Code inside region #endregion ExampleThe following example demonstrates the use of #region and #endregion preprocessor − using System;
class Program {
static void Main() {
Console.WriteLine("Code outside region");
#region Math Operations
int a = 10, b = 20;
int sum = a + b;
Console.WriteLine("Sum: " + sum);
#endregion
Console.WriteLine("Code outside region");
}
}
When the above code is compiled and executed, it produces the following result − Code outside region Sum: 30 Code outside region Advertisements
| ||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |