सी # प्रीप्रोसेसर निर्देश

इस ट्यूटोरियल में, हम Preprocessor Directives, उपलब्ध निर्देशों के बारे में C # में सीखेंगे, और कब, क्यों और कैसे इनका उपयोग किया जाता है।

जैसा कि नाम का औचित्य है, प्रीप्रोसेसर निर्देश कथन का एक खंड है जो वास्तविक संकलन शुरू होने से पहले संसाधित हो जाता है। C # प्रीप्रोसेसर निर्देश कंपाइलर के लिए कमांड हैं जो संकलन प्रक्रिया को प्रभावित करता है।

ये आदेश निर्दिष्ट करते हैं कि कोड के किन खंडों को संकलित करना है या विशिष्ट त्रुटियों और चेतावनियों को कैसे संभालना है।

C # प्रीप्रोसेसर निर्देश एक # (hash)प्रतीक से शुरू होता है और सभी प्रीप्रोसेसर निर्देश एक लाइन के लिए रहते हैं। प्रीप्रोसेसर निर्देश इसके new lineबजाय द्वारा समाप्त किए जाते हैं semicolon

C # में उपलब्ध प्रीप्रोसेसर निर्देश हैं:

C # में प्रीप्रोसेसर निर्देश
प्रक्रमण करने से पहले के निर्देश विवरण वाक्य - विन्यास
#if जाँचता है कि क्या एक प्रीप्रोसेसर अभिव्यक्ति सही है या नहीं
 #if को संकलित करने के लिए #if प्रीप्रोसेसर-एक्सप्रेशन कोड
#elif के साथ इस्तेमाल किया #ifकई पूर्वप्रक्रमक भाव जाँच करने के लिए
 #if प्रीप्रोसेसर-एक्सप्रेशन -1 कोड को कंपाइल करने के लिए # एलिफ प्रीप्रोसेसर-एक्सप्रेशन -2 कोड
#else #ifयौगिक सशर्त निर्देश बनाने के लिए उपयोग किया जाता है।
 #if प्रीप्रोसेसर-एक्सप्रेशन कोड #endif को संकलित करने के लिए #elif कोड संकलित करता है
#endif #ifएक सशर्त निर्देश के अंत को इंगित करने के साथ-साथ उपयोग किया जाता है
 #if को संकलित करने के लिए #if प्रीप्रोसेसर-एक्सप्रेशन कोड
#define एक प्रतीक को परिभाषित करने के लिए उपयोग किया जाता है
 # डेफिन सिंबल
#undef एक प्रतीक को अपरिभाषित करने के लिए उपयोग किया जाता है
 #undef SYMBOL
#warning हमें हमारे कोड से स्तर 1 चेतावनी उत्पन्न करने की अनुमति देता है
 # चेतावनी चेतावनी-संदेश
#error हमें हमारे कोड से त्रुटि उत्पन्न करने की अनुमति देता है
 # त्रुटि त्रुटि-संदेश
#line त्रुटियों और चेतावनियों को प्रदर्शित करने के लिए हमें संकलक की लाइन संख्या और फ़ाइल नाम को संशोधित करने की अनुमति देता है
 # लाइन लाइन-नंबर फ़ाइल-नाम
#region विजुअल स्टूडियो कोड एडिटर का उपयोग करते समय हमें एक ऐसा क्षेत्र बनाने की अनुमति दी जा सकती है जिसका विस्तार या पतन हो सकता है
 # भाग क्षेत्र-वर्णन कोड #endregion
#endregion एक क्षेत्र के अंत का संकेत देता है
 # भाग क्षेत्र-वर्णन कोड #endregion
#pragma उस फ़ाइल के संकलन के लिए संकलक को विशेष निर्देश देता है जिसमें वह दिखाई देता है।
 # प्रागम् प्रज्ञा-नाम प्रज्ञा-तर्क

# दिशा निर्देश

  • #defineनिर्देश हमें एक प्रतीक परिभाषित करने के लिए अनुमति देता है।
  • #ifनिर्देश के साथ उपयोग किए जाने पर परिभाषित किए गए चिह्न सत्य का मूल्यांकन करेंगे।
  • इन प्रतीकों का उपयोग संकलन के लिए शर्तों को निर्दिष्ट करने के लिए किया जा सकता है।
  • वाक्य - विन्यास:
     # डेफिन सिंबल
  • उदाहरण के लिए:
     #define टेस्टिंग
    यहां, TESTING एक प्रतीक है।

#undef निर्देश

  • #undefनिर्देश हमें एक प्रतीक परिभाषित करने के लिए अनुमति देता है।
  • #ifनिर्देश के साथ उपयोग किए जाने पर अपरिभाषित प्रतीक झूठे का मूल्यांकन करेंगे।
  • वाक्य - विन्यास:
     #undef SYMBOL
  • उदाहरण के लिए:
     #undef परीक्षण
    यहां, TESTING एक प्रतीक है।

#if निर्देश

  • #ifनिर्देश पूर्वप्रक्रमक अभिव्यक्ति का परीक्षण किया जाता है।
  • एक प्रीप्रोसेसर अभिव्यक्ति में केवल प्रतीकों का संयोजन या संचालकों &&( जैसे ), ||(OR), !(NOT) जैसे प्रतीकों का संयोजन हो सकता है ।
  • #ifनिर्देशन के बाद एक #endifनिर्देश होता है।
  • #ifनिर्देश के अंदर कोड केवल तभी संकलित किए जाते हैं, जब अभिव्यक्ति का परीक्षण #ifसत्य के मूल्यांकन के साथ किया जाता है।
  • वाक्य - विन्यास:
     #if प्रीप्रोसेसर-एक्सप्रेशन कोड को संकलन करने के लिए <#endif
  • उदाहरण के लिए:
    #if परीक्षण कंसोल। क्राइटलाइन ("वर्तमान में परीक्षण"); #अगर अंत

उदाहरण 1: #if निर्देश का उपयोग कैसे करें?

 #define CSHARP using System; namespace Directive ( class ConditionalDirective ( public static void Main(string() args) ( #if (CSHARP) Console.WriteLine("CSHARP is defined"); #endif ) ) ) 

जब हम प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 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 #elif directive 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 #elif directive is compiled only if the expression tested with that #elif evaluates 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 #else directive is used along with #if directive.
  • If none of the expression in the preceding #if and #elif (if present) directives are true, the codes inside the #else directive 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 #endif directive is used along with #if directive to indicate the end of #if directive.
  • 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 #warning directive 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 #error directive 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 #line directive 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 #region directive 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 #if block. However, a #region block can be included within a #if block and a #if block can overlap with a #region block.
  • #endregion directive indicates the end of a #region block.
  • 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 #pragma directive 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 #pragma instructions:
    • #pragma warning: Used for disabling or enabling warnings
    • #pragma checksum: It generates checksums for source files which will be used for debugging.
  • Syntax:
     # प्रागम् प्रज्ञा-नाम प्रज्ञा-तर्क
  • उदाहरण के लिए:
     #pragma चेतावनी अक्षम करें

उदाहरण 7: #pragma निर्देश का उपयोग कैसे करें?

 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 ) ) ) 

जब हम प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 Program.cs (12,22): चेतावनी CS1030: #warning: 'यह एक चेतावनी 2 है' (/home/myuser/csharp/directive-project/directive-project.csproj)

हम देख सकते हैं कि आउटपुट स्क्रीन पर केवल दूसरी चेतावनी प्रदर्शित होती है।

ऐसा इसलिए है, क्योंकि हमने शुरुआत में पहली चेतावनी से पहले सभी चेतावनियों को निष्क्रिय कर दिया था और दूसरी चेतावनी से पहले ही उन्हें बहाल कर दिया था। यही कारण है कि पहली चेतावनी छिपी हुई थी।

हम सभी चेतावनी के बजाय विशिष्ट चेतावनी को भी अक्षम कर सकते हैं।

अधिक जानकारी के लिए #pragma, #pragma (C # संदर्भ) पर जाएँ।

दिलचस्प लेख...