serving the solutions day and night


Sunday, May 8, 2011

Debug Directive

If the code is only for development environment not in the live or release environment use Debug Directive or Conditional attribute For exmple, Some of the code only run while in debugggin mode, but you don't want to shift to production server or code to run while in live

If the application/compilation running in the debug mode, then the debug code will execute, but if you complied the same code in the release mode, then debug code will not execute.

In Debug environment, output is
If Debug
No If
Conditional Debug
No Conditional

In Release environment, output is
If Release
No If
No Conditional

 class Program
        static void Main(string[] args)
            #if DEBUG
                Console.WriteLine("If Debug");
                Console.WriteLine("If Release");

            Console.WriteLine("No If");

            Program p = new Program();

        private void ConDebug()
            Console.WriteLine("Conditional Debug");

        private void NoCon()
            Console.WriteLine("No Conditional");

To Verify, the debug part in the release version or not,open Visual Studio Tools -> Visual Studio Command Prompt -> enter ildasm

File -> open Debug EXE or open Release EXE.

Debug exe

No comments: