TheDeveloperBlog.com

Home | Contact Us

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

C# Directives

These C# examples use preprocessor directives to influence program compilation. They cover 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.

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 then use #if directives to detect whether a #define was specified.

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

#define, undef

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

#if, elif, else and endif

Based on:

.NET 4.5

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.

#warning#error

Line. The line directive influences warnings and errors. The compiler will report line numbers based on your custom line directive.

#line

Pragma. The default compilation settings in Visual Studio are usually fine. But with pragma, you can adjust them. You can make some warnings disappear.

#pragma

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.

#region

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 help to read books about a subject. In the C# specification, I learned about the syntax of directives. These are "unsophisticated" macros.

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

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.


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