C# Preprocessor Directives

Introduction to C# Preprocessor Directives

Preprocessor directives special commands होती है जो compiler को दी जाती है। ये commands compiler द्वारा compilation से पूर्व process की जाती है और compilation process में बदलाव करती है।

Preprocessor commands आपको किसी निदेशक की भूमिका में ला देती है। जिस प्रकार एक निदेशक किसी फिल्म का direction करता है उसी प्रकार आप compilation process का direction कर पाते है। ये commands आपको compilation process पर पूरा control provide करती है।

उदाहरण के लिए आप decide कर सकते है की program के किस code को compilation process से exclude किया जाना चाहिए। साथ ही आप compiler को errors और warnings को handle करने के लिए भी direct कर सकते है।

C# में preprocessor directives का concept C language से लिया गया है। C और C++ languages में preprocessor directives को macros create करने के लिए use किया जाता है। लेकिन C# में ऐसा नहीं है। C# में preprocessor directives को मुख्यतः condition के आधार पर compilation के लिए प्रयोग किया जाता है।

सभी preprocessor commands # symbol के साथ शुरू होती है। Preprocessor commands कोई statement नहीं होती है इसलिए इन्हे semicolon द्वारा terminate नहीं किया जाता है। इन्हें new line द्वारा terminate किया जाता है। इसलिए एक line में सिर्फ एक ही preprocessor command define की जाती है।

C# में preprocessor directives को 3 श्रेणियों में बाँटा गया है। इनके बारे में निचे बताया जा रहा है।

  • Condition Directives – इस category की preprocessor commands conditional compilation के लिए प्रयोग की जाती है।
    • #if
    • #else
    • #elif
    • #endif
    • #define
    • #undef
  • Errors & Warnings Directives – इस category की preprocessor commands errors और warnings को control करने के लिए प्रयोग की जाती है।
    • #error
    • #warning
  • Other Directvies
    • #line
    • #pragma
    • #region
    • #endregion

ऊपर दिए सभी preprocessor directives के बारे में आगे detail से बताया जा रहा है।

#if

यह directive if statement की तरह ही कार्य करता है। इसे #endif directive के साथ प्रयोग किया जाता है। इस directive को define करने का syntax निचे दिया जा रहा है।

#if symbol

जैसा की आप ऊपर दिए गए syntax में देख सकते है #if directive के बाद condition के बजाय एक symbol define किया जाता है। यह symbol program में #define directive द्वारा define किया जाता है। इसके directive के बारे में आप आगे जानेंगे।

यदि define किया गया symbol program में available होता है #if और #endif directives के बीच दिया गया code execute हो जाता है।

#else

यह directive #if और #endif directives के साथ ही use किया जाता है। इन directives को एक साथ प्रयोग करके आप if else statement का structure create कर पाते है। इसका general syntax निचे दिया जा रहा है।

#if symbol
//code here…
#else
//code here
#endif

#else directive के बाद define किया गया code तब execute होता है जब #if directive के बाद define किया गया symbol program में available नहीं होता है।

#elif

यह directive elseif statement की तरह कार्य करता है। इस directive द्वारा आप #if के साथ एक से अधिक symbols को test कर सकते है और उसके अनुसार code execute कर सकते है। इस directive का general syntax निचे दिया जा रहा है।

#if symbol
//code here…
#elif other-symbol
//code here…
#elif another-symbol
//code here…
#endif

#endif

यह directive #if directive के साथ प्रयोग किया जाता है। इसे #if directive को end करने के लिए प्रयोग किया जाता है।

#define

यह directive symbol define करने के लिए प्रयोग किया जाता है। इस directive द्वारा define किये गए symbol के आधार पर ही compilation को control किया जाता है। Symbol #if directive define करते समय expression के रूप में use किया जाता है।

#define directive का general syntax निचे दिया जा रहा है।

#define symbol-here

#undef

यह symbol #define directive द्वारा define किये गए किसी भी symbol को undefine करने के लिए प्रयोग किया जाता है। जब आप इस directive द्वारा किसी symbol को undefine कर देते है तो उसके बाद उस symbol को use नहीं किया जा सकता है। इसका general syntax निचे दिया रहा है।

#undef symbol-here

#warning

इस directive द्वारा program में किसी specific location से warning generate की जा सकती है। यह directive compilation के समय किसी महत्वपूर्ण condition के बारे में सूचित करने के लिए उपयोगी होता है। इसे #if directive के बाद warning generate करने के लिए भी use किया जा सकता है।

#warning directive का general syntax निचे दिया जा रहा है।

#warning warning-message-here

#error

इस directive द्वारा program में किसी specific location से error generate की जा सकती है। इस directive को मुख्यतः #if directive द्वारा expression को evaluate करके error generate करने के लिए use किया जाता है। इसका general syntax निचे दिया जा रहा है।

#error error-message-here

#line

इस directive द्वारा program में output और errors के line numbers को modify किया जा सकता है। जब इस directive को normal define किया जाता है तो यह default line numbering show करता है। जब इस directive को hidden define किया जाता है तो output और errors के line numbers hide हो जाते है।

इसके अलावा इस directive को आप number के साथ भी define कर सकते है। जब आप number के साथ इस directive को define करते है तो आगे आने वाले statements को उसी number से start करके numbering की जाती है।

#line directive का general syntax निचे दिया जा रहा है।

#line default | hidden | number

#region

इस directive द्वारा आप ऐसा code block define कर सकते है जिसे expand और collapse किया जा सकता है। लेकिन यह तब ही संभव होता है जब आप visual studio code editor use कर रहे हो।

इस directive द्वारा define किये गए code block के pass एक arrow symbol show होता है जिसे click करने पर code collapse (hide) हो जाता है और वापस click करने पर expand हो जाता है।

#region directive उन situations में उपयोगी होता है जब आपका program बहुत बड़ा हो और उसमे बहुत अधिक code हो। उस situation में आप कुछ code blocks को hide कर सकते है और program में जरुरी parts पर ध्यान दे सकते है। इस directive द्वारा define किये गए block को #endregion द्वारा define किया जाना आवश्यक होता है।

#region directive का general syntax निचे दिया जा रहा है।

#region region-name-here

#endregion

#region directive द्वारा define किये गए code block को end करने के लिए इस directive का प्रयोग किया जाता है।

#pragma

यह directive compiler को compilation से सम्बंधित special instructions देने के लिए प्रयोग किया जाता है। दिए गए instructions compiler द्वारा supported होने चाहिए।

Microsoft C# compiler दो प्रकार के #pragma instructions को support करता है।

  • #pragma warning
  • #pragma checksum

#pragma warning

इस directive द्वारा कुछ specific warnings को enable और disable किया जा सकता है। इसके लिए disable command का प्रयोग किया जाता है। बाद में restore command द्वारा उन warnings को restore भी किया जा सकता है।

#pragma warning directive का general syntax निचे दिया जा रहा है।

#pragma warning disable warning-list
#pragma warning restore warning-list

#pragma checksum

यह directive ASP.NET pages की debugging के लिए source file का checksum generate करने के लिए प्रयोग किया जाता है। इसका syntax निचे दिया जा रहा है।

#pragma checksum “filename” “{guid}” “checksum bytes”

ऊपर दिए गए syntax में file name आपकी current file का नाम होता है और guid (Global Unique Identifier) आपकी file को uniquely identify करने के लिए id होती है।

Checksum bytes hexadecimal numbers की string होती है।

Example of C# Preprocessor Directives

C# में preprocessor directives के use को निचे उदाहरण द्वारा समझाया जा रहा है।

#define Best

using System;

class myClass
{
    static void Main(string[] args)
    {
        #if Best
        #warning Welcome to Best Hindi Tutorials;
        #endif
        Console.WriteLine(“Best Hindi Tutorials”);
    }
}

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

Compilation Succeeded -1 warning(s)
Best Hindi Tutorials

warning cs1030: #warning : 'Welcome to Best Hindi Tutorials';