štvrtok 27. júna 2013

Preprocessor directives vs. Conditional Attributes

This is a simple example where you can see proprocessor directive and also Conditional Attribute.




Code Snippet
     class Program
  1. {
  2.    static void Main(string[] args)
  3.    {
  4.  
  5. # if DEBUG
  6.      Console.WriteLine("Application starts in debug mode");
  7. # else
  8.     Console.WriteLine("Application starts NOT in debug mode");
  9. # endif
  10.  
  11.      SayHelloOnlyInDebug();
  12.  
  13.      Console.ReadLine();
  14. }
  15.  
  16.  [Conditional("DEBUG")]
  17.   private static void SayHelloOnlyInDebug()
  18.   {
  19.     Console.WriteLine("Hello!");
  20.   }
  21. }

       











































And what is  different between  proprocessor directive and Conditional Attribute?
Proprocessor directive - the statement in #if #else is compiled conditionally dependent upon thepresence of the DEBUG symbol. 
Conditional Attribute - You might feel more comfortable with the Conditional attribute, which can be used to exclude entire methods, without needing to complicate the source with conditionals on the calling side.





Žiadne komentáre:

Zverejnenie komentára