TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Define and Undef Directives

Use the #define and #undef preprocessor directives and test the defined constants.
Define, undef. The #define and #undef directives influence compilation. They should appear at the top of a source file. They can adjust compilation options for the entire file. As directives, they have no affect on runtime performance.Directives

Info: The undef directive is the opposite of the define directive. It doesn't matter if the symbol was never defined in the first place.

Example. We introduce the #define and #undef directives at the top of the file. We define the symbol B, then the symbol A, then undefine the symbol B. The program will compile with A being defined, and B being undefined.

Undef: In the code, the #undef B cancels out the #define B directive, leaving B not defined.

If: The #if directive is evaluated as preprocessing time, not runtime. It works in a similar way as the if-statement in the C# language itself.

If, Elif, Endif

Elif, Else: The #elif directive is the same as #if, except it must follow an #if or other #elif. The #else directive is the default case.

Endif: The #endif directive serves to terminate the #if #elif or #if #else directive structures.

C# program that define and undef directives // Define B, then define A. // ... You can use undef at the top here too. // ... Try changing A to C. #define B #define A #undef B using System; class Program { static void Main() { // Use an if/elif/endif construct. #if A Console.WriteLine("a"); #elif B Console.WriteLine("b"); #elif C Console.WriteLine("c"); #endif // Use an if/else/endif construct. #if A Console.WriteLine("a2"); #else Console.WriteLine("!a2"); #endif } } Output a a2
Notes, symbols. In the C# language, #define is considered a preprocessing directive. There are some invalid syntaxes for defined symbols. You cannot use a number value as the defined identifier, for example.

Tip: Instead of reading the language specification, you can just experiment in the compiler to see what works.

Directives. The term "directive" indicates a line in the program that is not part of the program's execution logic. Directives provide hints about how the program is compiled, not what it does with instructions and the evaluation stack.

Info: In the C# language, the "using System" line is also a directive, but not a preprocessing directive.

Using System

Note: Please see section 9.5 of the ECMA-344 "C# Language Specification", "Pre-processing directives" for a complete reference.

Summary. We demonstrated the #define and #undef preprocessing directives. These provide a way to conditionally compile or remove parts of the source text. These directives can be a clue that a file is overly complex or not well-organized.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf