Monday, 26 March 2018

040 C# Preprocessor Directives

Preprocessor directive
Preprocessor directive gives instruction compiler , before the actual compilation starts.  Preprocessor directives begin with #, but do not ends with semicolon (;) . C and C++  have separate processor, which create macros . C# do not have any separate Preprocessor.  C# considers Preprocessor directive as  instruction.

A simple program with Preprocessor directive

Example  
#define mySysmbol
using System;
public class Program
{
              public static void Main()
               {
                   #if (mySysmbol)
                      Console.WriteLine("mySysmbol is defined");
                  #endif
                 Console.ReadKey();
            }
}
 Output :
mySysmbol is defined

Here is the most common used Preprocessor directive.

directive What is ?
#define define symbol Preprocessor. The symbol maybe sequence of character
#undefine undefine the symbol which was defined by the #define.
#if test or check particular symbol or symbol with compound statement with operator return true or false. If true is return , some statement is executed
#else if #if returns false , this code block is executed with some statement
#elif checking of compound conditional statement on define by the #define
#endif close or end code block started from #if directives
#line compiler line number can be customize
#error customized error , is generated from desired location
#warning customize warning is generated from desired location
#region specify an area code blocks. Expand and collapse functionalities is by defauly in .Net framework
#endregion end code block area define by #define

A Preprocessor directive is defined at the top of a program as it is defined before the compilation of program . Preprocessor directive symbols  and same name variable can be declared, both do not conflict each other.

A simple program with Preprocessor directive , directive symbol and variable are same name.

Example  

#define mySysmbol
using System;
public class Program
{
          public static void Main()
         {
                    string mySysmbol = string.Empty;
                    mySysmbol = "I Am Learning C#";

                   #if (mySysmbol)
                               Console.WriteLine("mySysmbol is defined");
                  #endif
                  Console.ReadKey();
           }
}
Output :
mySysmbol is defined

If you are writing check statement (if) , you can use operator like =, == ,!=,&&,||,! . #region  is generally written when long term code is written , code is  separated  with different part of logic.

A simple program with Preprocessor directive , with operator.

Example
#define mySysmbol1
#define mySysmbol2
using System;
            public class Program
            {
                          public static void Main()
                         {
                                #if (mySysmbol1 && mySysmbol2)
                                {
                                           Console.WriteLine("mySysmbol1 and mySysmbol2 defined");
                                }
                               #endif
                               Console.ReadKey();
                    }
}
Output :
mySysmbol1 and mySysmbol2 defined



No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...