Preprocessor directive in C #
Preprocessor Directive directives provide instructions to the compiler to preprocess the information before the actual compilation process begins.
All preprocessor directives in C # start with #, and only white-space characters can appear before a preprocessor directive in a line. Preprocessor directives in C # are not commands, so they do not end with a semicolon (;).
The C # compiler does not have a separate preprocessor, however, these instructions are handled as if there was actually a separate preprocessor. In C #, preprocessor directives are used to help conditional compilation. Unlike preprocessor directives in C and C ++, they are not used to create macros. A mandatory preprocessor directive is a single instruction on a line.
Preprocessor directives in C #
Below is a list of preprocessing directives available in C #:
Preprocessor directive #define It defines a sequence of characters, called symbols. #undef It allows you to not define (undefine) an icon. #if It allows checking whether an icon or multiple icons is true. #else It allows creating a conditional directive, along with #if. #elif It allows creating a conditionally complex directive. #endif Identify the end of a conditional directive. #line It allows you to modify the line number of the compiler and (optionally) the file name for Error and Warning. #error It allows to create an error from a specific location in your code. #warning It allows creating a warning level from a specific location in your code. #region It allows you to define a code block that you can expand or collapse using the Visual Studio Code Editor feature. #endregion It marks the end of a #region block.#Define preprocessor directive in C #
#Define preprocessor directive in C # creates symbol constants.
#define allows you to create an icon so that, when using an expression symbol passed to #if preprocessor directive, the expression evaluates to true. Its syntax is as follows:
define symbol
The following example illustrates this:
#define PI using System ; namespace PreprocessorDAppl { class QTM { static void Main ( string [] args ) { #if (PI) Console . WriteLine ( "PI đã được định nghĩa" ); #else Console . WriteLine ( "PI chưa được định nghĩa" ); #endif Console . ReadKey (); } } }
When running the C # program, we have the following results:
PI has been defined
Conditional Directive in C #
You can use the #if preprocessor directive in C # to create a Conditional Directive. Conditional directives are useful when checking an icon or icons to check if they estimate to true. If they evaluate to true, the compiler estimates all code between the #if directive and the next directive.
Syntax for conditional directives in C # is:
if symbol [operator ký hiệu] .
Here, symbol is the name of the symbol you want to check. You can also use true and false or append the following symbol to the negative operator.
operator symbol is the operator used to estimate the symbol. Operators can be one of:
- == (equals)
- ! = (not equal)
- && (and)
- || (or)
You can also group symbols and operators by parentheses. Conditional directives are used to compile code for debugging or when compiling for a specific configuration. A conditional directive in C # starts with a #if preprocessor directive that must be explicitly terminated by a #endif directive.
The following example illustrates the use of conditional directives in C #:
#define DEBUG #define VC_V10 using System ; public class Tester { public static void Main () { #if (DEBUG && !VC_V10) Console . WriteLine ( "DEBUG đã được định nghĩa" ); #elif (! DEBUG && VC_V10 ) Console . WriteLine ( "VC_V10 đã được định nghĩa" ); #elif ( DEBUG && VC_V10 ) Console . WriteLine ( "DEBUG và VC_V10 đã được định nghĩa" ); #else Console . WriteLine ( "DEBUG và VC_V10 đã được định nghĩa" ); #endif Console . ReadKey (); } }
Running the C # code above gets the following result:
DEBUG and VC_V10 have been defined
According to Tutorialspoint
Previous article: Namespace in C #
Next lesson: Regular Expression in C #
You should read it
May be interested
- Will the Internet change forever after European Copyright Law?on 12 september 2018, the european parliament adopted the new law, entitled copyright directive. accordingly, in europe online service companies will have to prevent users from posting pirated content online and have to share revenue with news, video and music producers.
- Create the table in AngularJStable data is often repeated in natural order. in angularjs, ng-repeat directive can be used to draw tables easily. below is an example of how to use ng-repeat to create tables.
- View components in AngularJSangularjs supports single page application through multiple views on a single page. to do this, angularjs provides ng-view and ng-template directive and $ routeprovider service.
- Can Pokemon Go be played on a computer? The answer is yes!by software emulation and additional software to support positioning, we can completely play pokemon go right on the computer, comfortable catching pokemon without worrying about running out of battery when playing as on the phone.
- Summary - How to play Pokemon GO, virtual reality game catch Pokemon on smartphonepokemon go game (styled, raising and training virtual animals) has become popular, the online community with new movements also immediately responded to this mobile game in the direction positive direction
- Things you didn't know about 26 types of Pokeball - Part 2to be able to catch pokemon, players need to use pokeball. the higher the ball, the higher the success rate of pokemon
- Stardust's effective way in Pokemon GOpokemon go stardust is used to evolve pokemom with candy. therefore, if there is more stardust, it will make the evolution easier.
- AZ word about eggs in Pokemon Gothere are many different pokemon eggs that can hatch into many pokemon, but to make these eggs hatch into pokemon, you will have to perform some tasks. each player begins with an egg incubator, which allows the user to incubate an egg after performing certain steps, gps and pedometer tracking on the mobile device of you and the game.
- Things you didn't know about 26 types of Pokéball - Part 1pokéball is the most important item in pokémon go, allowing gamers to quickly catch pokémon back to their team. the higher the pokéball, the more effective the ability to catch pokémon.
- 10 tips to help you become a great Pokemon trainerin the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...