Selles õpetuses õpime tundma eeltöötluse direktiive, C-s saadaval olevaid direktiive ja millal, miks ja kuidas neid kasutatakse.
Nagu nimi õigustab, on eeltöötlusdirektiivid lausete plokk, mida töödeldakse enne tegeliku kompileerimise algust. C # eeltöötlusdirektiivid on kompilaatori käsud, mis kompileerimisprotsessi mõjutavad.
Need käsud määravad, millised koodi jaotised kompileerida või kuidas konkreetseid vigu ja hoiatusi käsitleda.
C # eeltöötleja direktiiv algab # (hash)sümboliga ja kõik eeltöötleja direktiivid kestavad ühe rea. Eeltöötlusdirektiivid lõpetab new linepigem kui semicolon.
Eeltöötleja direktiivid, mis on saadaval C # keeles, on:
Eeltöötleja direktiivid C #| Eeltöötleja direktiiv | Kirjeldus | Süntaks | 
|---|---|---|
| #if | Kontrollib, kas eelprotsessori avaldis vastab tõele või mitte | #if eeltöötleja-avaldise kood #endif kompileerimiseks | 
| #elif | Kasutatakse koos #ifmitme eelprotsessori avaldise kontrollimiseks | #if preprocessor-expression-1 kood koostamiseks #elif preprocessor-expression-2 kood koostamiseks #endif | 
| #else | Kasutatakse koos #iftingimusliku liitdirektiivi loomiseks. | #if eeltöötleja-avaldise kood #elif-koodi koostamiseks #endif-i kompileerimiseks | 
| #endif | Kasutatakse koos #iftingimusliku direktiivi lõpu tähistamiseks | #if eeltöötleja-avaldise kood #endif kompileerimiseks | 
| #define | Kasutatakse sümboli määratlemiseks | #define SÜMBOL | 
| #undef | Kasutatakse sümboli määratlemata jätmiseks | #undef SÜMBOL | 
| #warning | Võimaldab meil oma koodist genereerida 1. taseme hoiatuse | #warning hoiatussõnum | 
| #error | Võimaldab meil oma koodist viga genereerida | #error tõrketeade | 
| #line | Võimaldab meil kompilaatori rea numbrit ja failinime muuta, et kuvada vigu ja hoiatusi | # line rea number faili nimi | 
| #region | Võimaldab meil luua piirkonna, mida saab Visual Studio koodiredaktori kasutamisel laiendada või ahendada | #region regiooni kirjelduskoodid #regregion | 
| #endregion | Näitab piirkonna lõppu | #region regiooni kirjelduskoodid #regregion | 
| #pragma | Annab koostajale erijuhised selle faili koostamiseks, milles see ilmub. | #pragma pragma-nime pragma-argumendid | 
#define direktiiv
- #defineDirektiiv võimaldab meil määratleda sümbol.
- Sümbolid, mis on määratletud koos #ifdirektiiviga, peavad paika.
- Neid sümboleid saab kasutada kompileerimise tingimuste täpsustamiseks.
- Süntaks: 
#define SÜMBOL 
- Näiteks: 
#define TESTIMINE Siin on TESTIMINE sümbol.
#undef direktiiv
- #undefDirektiiv võimaldab meil undefine sümbol.
- Määramata sümbolid, kui neid kasutatakse koos #ifdirektiiviga, hindavad valet.
- Süntaks: 
#undef SÜMBOL 
- Näiteks: 
#undef TESTIMINE Siin on TESTIMINE sümbol.
# kui direktiiv
- #ifDirektiiv testimiseks kasutatakse preprotsessoris ekspressiooni.
- Eeltöötleja avaldis võib koosneda ainult sümbolist või sümbolite kombinatsioonist koos selliste operaatoritega nagu &&(AND),||(OR),!(NOT).
- #ifdirektiivile järgneb- #endifdirektiiv.
- Koodid #ifdirektiivi sees koostatakse ainult siis, kui testitud avaldise väärtus#ifvastab tõele.
- Süntaks: 
#if eeltöötleja-avaldise kood kompileerimiseks <#endif 
- Näiteks: 
#if TESTIMINE Console.WriteLine ("Praegu testitakse"); #endif
Näide 1: Kuidas kasutada #if-direktiivi?
 #define CSHARP using System; namespace Directive ( class ConditionalDirective ( public static void Main(string() args) ( #if (CSHARP) Console.WriteLine("CSHARP is defined"); #endif ) ) ) 
Programmi käivitamisel on väljund:
CSHARP is defined
In the above program, CSHARP symbol is defined using the #define directive at the beginning of program. Inside the Main() method, #if directive is used to test whether CSHARP is true or not. The block of code inside #if directive is compiled only if CSHARP is defined.
#elif directive
- The #elifdirective is used along with #if directive that lets us create a compound conditional directive.
- It is used when testing multiple preprocessor expression.
- The codes inside the #elifdirective is compiled only if the expression tested with that#elifevaluates to true.
- Syntax: 
#if preprocessor-expression-1 code to compile #elif preprocessor-expression-2 code-to-compile #endif 
- For example: 
#if TESTING Console.WriteLine("Currently Testing"); #elif TRAINING Console.WriteLine("Currently Training"); #endif
#else directive
- The #elsedirective is used along with#ifdirective.
- If none of the expression in the preceding #ifand#elif(if present) directives are true, the codes inside the#elsedirective will be compiled.
- Syntax: 
#if preprocessor-expression-1 code to compile #elif preprocessor-expression-2 code-to-compile #else code-to-compile #endif 
- For example: 
#if TESTING Console.WriteLine("Currently Testing"); #elif TRAINING Console.WriteLine("Currently Training"); #else Console.WriteLine("Neither Testing nor Training"); #endif
#endif directive
- The #endifdirective is used along with#ifdirective to indicate the end of#ifdirective.
- Syntax: 
#if preprocessor-expression-1 code to compile #endif 
- For example: 
#if TESTING Console.WriteLine("Currently Testing"); #endif
Example 2: How to use conditional directive (if, elif, else, endif) ?
 #define CSHARP #undef PYTHON using System; namespace Directive ( class ConditionalDirective ( static void Main(string() args) ( #if (CSHARP && PYTHON) Console.WriteLine("CSHARP and PYTHON are defined"); #elif (CSHARP && !PYTHON) Console.WriteLine("CSHARP is defined, PYTHON is undefined"); #elif (!CSHARP && PYTHON) Console.WriteLine("PYTHON is defined, CSHARP is undefined"); #else Console.WriteLine("CSHARP and PYTHON are undefined"); #endif ) ) )
When we run the program, the output will be:
CSHARP is defined, PYTHON is undefined
In this example, we can see the use of #elif and #else directive. These directive are used when there are multiple conditions to be tested. Also, symbols can be combined using logical operators to form a preprocessor expression.
#warning directive
- The #warningdirective allows us to generate a user-defined level one warning from our code.
- Syntax: 
#warning warning-message 
- For example: 
#warning This is a warning message 
Example 3: How to use #warning directive?
 using System; namespace Directives ( class WarningDirective ( public static void Main(string() args) ( #if (!CSHARP) #warning CSHARP is undefined #endif Console.WriteLine("#warning directive example"); ) ) ) 
When we run the program, the output will be:
Program.cs(10,26): warning CS1030: #warning: 'CSHARP is undefined' (/home/myuser/csharp/directives-project/directives-project.csproj) #warning directive example
After running the above program, we will see the output as above. The text represents a warning message. Here, we are generating a user-defined warning message using the #warning directive.
Note that the statements after the #warning directive are also executed. It means that the #warning directive does not terminate the program but just throws a warning.
#error directive
- The #errordirective allows us to generate a user-defined error from our code.
- Syntax: 
#error error-message 
- For example: 
#error This is an error message 
Example 4: How to use #error directive?
 using System; namespace Directive ( class Error ( public static void Main(string() args) ( #if (!CSHARP) #error CSHARP is undefined #endif Console.WriteLine("#error directive example"); ) ) ) 
When we run the program, the output will be:
Program.cs(10,24): error CS1029: #error: 'CSHARP is undefined' (/home/myuser/csharp/directives-project/directives-project.csproj) The build failed. Please fix the build errors and run again.
We will see some errors, probably like above. Here we are generating a user-defined error.
Another thing to note here is the program will be terminated and the line #error directive example won't be printed as it was in the #warning directive.
#line directive
- The #linedirective allows us to modify the line number and the filename for errors and warnings.
- Syntax: 
#line line-number file-name 
- For example: 
#line 50 "fakeprogram.cs" 
Example 5: How to use #line directive?
 using System; namespace Directive ( class Error ( public static void Main(string() args) ( #line 200 "AnotherProgram.cs" #warning Actual Warning generated by Program.cs on line 10 ) ) ) 
When we run the program, the output will be:
AnotherProgram.cs(200,22): warning CS1030: #warning: 'Actual Warning generated by Program.cs on line 10' (/home/myuser/csh arp/directive-project/directive-project.csproj)
We have saved the above example as Program.cs. The warning was actually generated at line 10 by Program.cs. Using the #line directive, we have changed the line number to 200 and the filename to AnotherProgram.cs that generated the error.
#region and #endregion directive
- The #regiondirective allows us to create a region that can be expanded or collapsed when using a Visual Studio Code Editor.
- This directive is simply used to organize the code.
- The #region block can not overlap with a #ifblock. However, a#regionblock can be included within a#ifblock and a#ifblock can overlap with a#regionblock.
- #endregiondirective indicates the end of a- #regionblock.
- Syntax: 
#region region-description codes #endregion 
Example 6: How to use #region directive?
 using System; namespace Directive ( class Region ( public static void Main(string() args) ( #region Hello Console.WriteLine("Hello"); Console.WriteLine("Hello"); Console.WriteLine("Hello"); Console.WriteLine("Hello"); Console.WriteLine("Hello"); #endregion ) ) ) 
When we run the program, the output will be:
Hello Hello Hello Hello Hello
#pragma directive
- The #pragmadirective is used to give the compiler some special instructions for the compilation of the file in which it appears.
- The instruction may include disabling or enabling some warnings.
- C# supports two #pragmainstructions:- #pragma warning: Used for disabling or enabling warnings
- #pragma checksum: It generates checksums for source files which will be used for debugging.
 
- Syntax: 
#pragma pragma-nime pragma-argumendid 
- Näiteks: 
#pragma hoiatus keelata 
Näide 7: Kuidas kasutada #pragma direktiivi?
 using System; namespace Directive ( class Error ( public static void Main(string() args) ( #pragma warning disable #warning This is a warning 1 #pragma warning restore #warning This is a warning 2 ) ) ) 
Programmi käivitamisel on väljund:
Program.cs (12,22): hoiatus CS1030: #warning: 'See on hoiatus 2' (/home/myuser/csharp/directive-project/directive-project.csproj)
Näeme , et väljundiekraanil kuvatakse ainult teine hoiatus .
Selle põhjuseks on see, et algselt keelasime kõik hoiatused enne esimest hoiatust ja taastasime need alles enne teist hoiatust. See on põhjus, miks esimene hoiatus peideti.
Võime kõigi hoiatuste asemel ka konkreetse hoiatuse keelata.
Lisateabe saamiseks #pragmakülastage #pragma (C # viide).








