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# Directives

Use preprocessor directives to influence program compilation. Test define, if and region.
Directives. Compilers operate in phases. In an early phase the program is read. Statements called preprocessor directives are handled.
With symbols like #define, directives can add or remove lines of code, without affecting runtime. Many programs can reside in one. Things become complex.#define, undef#error, #warning#if, elif, else and endif#line#pragma#region, #endregion
An example. This program uses a #define directive. It defines a symbol named PERL. This needs to be at the top of the source code file.

Then: We can use #if directives in the rest of the file to detect whether a #define was specified.

Here: The program shows that #if PERL will evaluate to true. But #if PYTHON will not.

If: We conditional directives, such as "if" and "elif," and we test the values of our definitions.

C# program that uses define, preprocessor directives #define PERL using System; class Program { static void Main() { #if PERL Console.WriteLine(true); #endif #if PYTHON Console.WriteLine(false); #endif } } Output True
Warning, error. The warning and error directives make the compiler complain. This is helpful when generating code—you can force an error to occur if the code is somehow incorrect.
Line. The line directive influences warnings and errors. The compiler will report line numbers based on your custom line directive.
Pragma. The default compilation settings in Visual Studio are usually fine. But with pragma, you can adjust them. You can make some warnings disappear.

So: If you know about a warning and it just annoys you, pragma can help. Sometimes this helps with an unused code warning.

However: The pragma directive seems, in my experience, just to add further complexity to an issue that is already complex.

Region. This directive organizes code. We create named "regions" of code, terminated with endregion. We can then collapse these regions in Visual Studio.

Caution: Other organizational constructs are often better. For example, consider using extra classes or methods to break up your code.

Chaos. Directives lead to code maintenance problems. Bit rot can begin if you use "#if" to remove some code blocks. The program may change so that the code no longer works.

And: Even if the code works, it will not have been tested. Things become more difficult, more dangerous for the programmer.

Bit rot: In large programs, this is a serious issue. Code becomes neglected and might stop working. But in its neglect, we don't know this.

Unreachable: With the pragma directive, we can keep unused code compiling with no warnings. This might help reduce bit rot.

Unreachable
Research. It always helps to read books about a subject. In the C# specification, I learned about the syntax of directives. These are "unsophisticated" macros.

Quote: A preprocessing directive always occupies a separate line of source code and always begins with a "#" character and a preprocessing directive name (The C# Programming Language).

Quote: The preprocessor is a relatively unsophisticated macro processor that works primarily on lexical tokens (The C++ Programming Language).

Many useful directives exist in the C# language. Directives, along with comments, have no effect on program runtime. They instead change how a program text is compiled.
© 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