The Precompiler C#

The precompiler is present for a long time in languages such as C/C++, it is used to solve the problem of differentiation of its basic software, such as a code written for multiple operating systems. Consider a trivial problem, write a function that creates a file and insert a sentence. The processes of input and output are different from system to system, that’s why you use the precompiler directives for different versions of the same program.
With the introduction of the bytecode it all seems useless, because the compiler produces code that runs on a virtual machine differs depending on the operating system, which is why he has not adopted early Java, C# on the contrary. In fact, the precompiler instructions can be used to create different versions of the program, such as the debug version, demo, trial or complete. But we see the precompiler directives that can be used in C#.
The main directive of the #define preprocessor is you need to define a label that you can verify their existence. With #if, #elif, #else and #endif is possible to manage the definition of a label, here is a small example.

#define DESKTOP

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestCons
{
  class Program
  {
    static void Main(string[] args)
    {
#if DESKTOP
      System.Console.WriteLine("This is desktop version!");
#elif SERVER
      System.Console.WriteLine("This is server version!");
#else
      System.Console.WriteLine("This is another version!");
#endif
     }
   }
}

The construct #if serves to create conditional branches within the code, since it is precompilation, it is not possible to vary the code at runtime, since the non-incorporated parts are not compiled and then in the final compiled code, there are no.
Using the #line directive you can change the location information in the compilation of the source code. For example, if we insert this row:

#line 100 “test.cs”

the precompiler will begin to re-count the rows as if we were on line 100. For unforce just enter:

#line default

this line the compiler will continue forward with the correct numbering.
The #warning and #error are just what they seem, the first displays a warning message during compilation, and the second stop filling completely.
A directive is pretty useless #region which is used to define a region of source code, the only purpose of it is to work with the editor on only one part of the code and can help with the indentation of the same.
In the latest versions of C# were also included preprocessor directives may be more important in C/C++, the #pragma that can have many uses. For example, the line:

#pragma warning disable 3021

this line the compiler will continue forward with the correct numbering.
disables the display of warning with number 3021 compilation is useful when you know the extent of the problem, and then you avoid dirty compilation output. To restore the vision of warning we can write:

#pragma warning restore 3021

and after this line the compiler will insert it again in the output of the compilation warning message 3021.
For a programmer like me, born with the C/C++ preprocessor directives are very important, in fact the normally use to hide alerts that I already know, and in this way I can have the attention only on issues whose consequences do not know, have this tool in C# is very important. Finally, the precompiler directives are fundamental to the conditional compilation and creation of programs of different versions without any effort, in fact, in C#, you can enter the label you want to define and then create several completed by simply changing the subject on the command line compiler.

This entry was posted in C# (sharp). Bookmark the permalink.