Preprocessor directive in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 2707 red flag

How to use various preprocessor directive in C#

Preprocessor directive in C#

Preprocessor directive supplies information to compiler before to compile program. And depending on preprocessor directive compiler takes decision and generate most optimize code for processing.

Preprocessor directive starts with # symbol and as it not programs statement so it does not end with semicolon (;).

There are many preprocessor directives in C# and in this article we will see few most popular of them.

Let us clear one important concept at first “Compiler takes decision in compile time with the help of preprocessor directive”. Yes, in compile time compiler evaluate value of preprocessor directive. Let us prove that in below example.

#define Sourav
using System;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            #if (Sourav)
               Console.WriteLine("Sourav is defined");
            #else
                        Console.WriteLine("PI is not defined");
            #endif
 
        }
    }
}

In this program we have define Sourav in first line of program. Now when we have written if and else condition, automatically statement in else condition became blur (in active). So from that its clear compiler is taking decision in compile time. Now we will see few more example of preprocessor directive.

#define

Using #define we can define any constant in program. Try to understand below example.

#define A
using System;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            #if(A)
                Console.WriteLine("A is defined somewhere");
            #endif
 
                Console.ReadLine();
        }
    }
}
 


#undef

Using #undef we can undefined any predefined constant. Let’s see below example.  

#define A
#undef A
using System;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
          
            #if(B)
              Console.WriteLine("A is defined somewhere");
            #endif
            Console.WriteLine("A is not defined");
          
            Console.ReadLine();
        }
    }
}


Here in first line we have defined A and in second line we are just making it undefined forcefully. And within Main() we are checking whether A is defined or not ? And output is saying A is not defined in program.


#error

Error directive is used to generate error in program. Let’s see below example.

 

 

 

In first the constant TrialVersion is defined and within if condition we are checking whether the version is trial or not?

And when we will compile this code it will show same error what we have defined within error directive. We can see red color underline below of message.

 

#warning

Like error message, we can show user defined warning in code. Let’s see below code.

 

 

As we have used warning directive, it’s showing green underline below of warning message

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)