C# Preprocessor Directives: C/C++ की तरह ही हम C# Program में भी Preprocessor Directives को Use करके Program के Compilation को Control कर सकते हैं।
उदाहरण के लिए जब हम कोई Professional Application Develop कर रहे होते हैं, तो किसी एक ही तरह की जरूरत को पूरा करने के लिए कई बार हम दो या दो से अधिक तरह के Codes लिख लेते हैं। क्योंकि Development Stage में हमें हमारी जरूरत के अनुसार कई तरह के Codes लिखने होते हैं। लेकिन जब Production की स्थिति आती है, तब हमें Best Performing Code को ही Compiler द्वारा Compile करवाना होता है, जबकि अन्य Codes को हमें उस Program से Remove करना पडता है।
लेकिन कभी-कभी Situation ऐसी होती है कि हम एक ही काम को करने के लिए लिखे गए दो अलग Codes को एक ही Program में रखना चाहते हैं, क्योंकि हमें लगता है कि भविष्य में कभी पहले Code के स्थान पर हमें दूसरे Code की जरूरत जरूर पडेगी।
इस तरह की स्थिति में हम सामान्यत: अपने ऐसे Code को Comment की तरह अपने Program में Specified ही रखते हैं, ताकि भविद्गय में जब भी जरूरत पडे, उस Code को Uncomment करके उपयोग में लिया जा सके।
लेकिन C# हमें ये सुविधा देता है कि हम हमारे Program की Coding द्वारा ही C# Compiler को इस बात का Instruction दे सकते हैं कि उसे कौनसा Code Compile करना है और कौनसा नहीं।
C# हमें जो Word Phrase Provide करता है, जिनके आधार पर हम इस बात को तय करते हैं कि Compiler किस स्थिति में किस Code को Compile करेगा और किस स्थिति में किस Code Block को Compile नहीं करेगा, उन Word Phrase को Preprocessor Directives के नाम से जाना जाता है।
C# मूल रूप से निम्न Preprocessors को Support करता है, जिनके आधार पर हम हमारे Program की Conditional Compiling कर सकते हैं:
#define | #elif | #else | #endif |
#endregion | #error | #if | #line |
#pragma | #region | #undef | #warning |
साथ ही सभी C# Preprocessor Directives को Use करते समय हमें कुछ General Rules को Follow करना जरूरी होता है जो कि निम्नानुसार हैं:
- सभी Preprocessor Directives को Program में एक अलग Line में Specify करना होता है। यानी हम एक Line में एक से ज्यादा Preprocessor Directives को Use नहीं कर सकते।
- सभी Preprocessor Directives की शुरूआत # Special Symbol से होती है।
- सभी Preprocessor Directives के # Symbol से पहले व बाद में Whitespaces हो सकते हैं।
- सभी Preprocessor Directives के अन्त में Comments का प्रयोग किया जा सकता है।
- सभी Preprocessor Directives का अन्त Semicolon से नहीं होता।
C# के विभिन्न Preprocessor Directives व उनके Meaning को Summary के रूप में निम्नानुसार सारणी के रूप में Represent किया जा सकता है:
Directive | Summary of Meaning |
#define identifier | Defines a compilation symbol. |
#undef identifier | Undefines a compilation symbol. |
#if expression | If the expression is true, the compiler compiles the following section. |
#elif expression | If the expression is true, the compiler compiles the following section. |
#else | If the previous #if or #elif expression is false, the compiler compiles the following section. |
#endif | Marks the end of an #if construct. |
#region name | Marks the beginning of a region of code; has no compilation effect. |
#endregion name | Marks the end of a region of code; has no compilation effect. |
#warning message | Displays a compile-time warning message. |
#error message | Displays a compile-time error message. |
#line indicator | Changes the line numbers displayed in compiler messages. |
#pragma text | Specifies information about the program context. |
C# Preprocessor Directives: #define and #undef
C# में जितने भी Compilation Symbols को Use किया जा सकता है, वे सभी Identifiers हैं, जो या तो Defined हो सकते हैं अथवा Undefined हो सकते हैं। true व false के अलावा हम किसी भी Identifier यानी नाम को इन Directives के साथ Identifier के रूप में Use कर सकते हैं। यहां तक कि हम C# के Keywords को व किसी Program में Define किए गए अन्य Identifies को भी इन Directives के लिए उपयोग में ले सकते हैं।
जब हम #define Directive को Use करते हैं, तो हम किसी Compilation Symbol को Define कर रहे होते हैं और जब हम #undef Directive को Use करते हैं, तो हम किसी Compilation Symbol को Undefined कर रहे होते हैं।
#define EXPERIMENTAL
#define PRODUCTION
// Code Here
#undef EXPERIMENTAL
इन दोनों Directives को किसी Program को Source File के Top Side में ही Use किया जा सकता है जबकि किसी Source File के सभी #define Directives को Specify करने के बाद ही C# Codes की शुरूआत की जा सकती है। जब एक बार हम C# Code लिखना शुरू कर देते हैं, उसके बाद हम इन Directives का प्रयोग करके नया Compilation Symbol Define नहीं कर सकते। जैसे:
using System; // First line of C# code
#define EXPERIMENTAL // Error
namespace Eagle
{
#define PRODUCTION // Error
. . .
}
Compilation Symbol का Scope केवल Current File ही होता है। यानी हम एक File में Define किए गए किसी Compilation Symbol यानी Compilation Identifier को किसी दूसरी Source File में Use नहीं कर सकते और यदि करना चाहते हैं, तो हमें उसी नाम का दूसरा Compilation Symbol Redefine करना होता है।
हालांकि हम #define का प्रयोग करके अपनी Source File के सभी Compilation Symbols को File के Top में Define करते हैं, लेकिन हम #undef Directive का प्रयोग करके पूरे Program के दौरान कहीं पर भी किसी भी #define के माध्यम से Define किए गए Compilation Symbol को Un-Define कर सकते हैं।
यानी जब हम किसी Defined Symbol को अपने Source Program में किसी विशेष Location के बाद आगे उपयोग में लेना नहीं चाहते, तो हम उस Specific Symbol को उस Particular Location से Un-Define कर सकते हैं और किसी Symbol को Undefined करने के लिए हमें उस Symbol को #undef Directive के साथ Use करना होता है।
जब हम किसी Symbol को किसी Location पर Undefined कर देते हैं, तो उस Symbol को उस Particular Location से आगे उपयोग में नहीं लिया जा सकता। यदि हम किसी Symbol को Undefined करने के बाद उसे उपयोग में लेने की कोशिश करें, तो C# Compiler Error Generate करता है।
ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook C#.NET in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी।
C#.NET in Hindi | Page:908 | Format: PDF