#ifndef _DEBUG equivalent...

  • Thread starter Thread starter Daniel Bass
  • Start date Start date
D

Daniel Bass

In C++ you can use the following:

#ifndef _DEBUG

// code here only runs in debug mode

#endif

where you can set different code to be included in different builds. Useful
for having a logging system for debugging only when compiling the project in
Debug, but wanting to turn it off in Release.

It this possible with c#.net? (VS.net 2003)

Thanks!
Dan.
 
In C++ you can use the following:

#ifndef _DEBUG

// code here only runs in debug mode

#endif

I think you meant:

#ifdef _DEBUG

there.

You can do that with C# too:

#if DEBUG
....
#endif
where you can set different code to be included in different builds. Useful
for having a logging system for debugging only when compiling the project in
Debug, but wanting to turn it off in Release.

For that kind of thing it's actually easier to use an attribute on the
method you're calling - look at the Trace class for an example.
 
Also, if you use the Debug statements "Debug.WriteLine", "Debug.*", the c#
and vb.net compilers automatically ignore these statements in release
builds.

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]
 
That's not exactly true; the Debug.Write statement will be compiled and
called in both Debug and Release builds. The difference is that in Release
builds the function won't actually do anything as TraceInternal class won't
have any listeners registered.

This means you will still have a (slight) overhead in Release builds
whenever Debug.Write is called.

Eric Newton said:
Also, if you use the Debug statements "Debug.WriteLine", "Debug.*", the c#
and vb.net compilers automatically ignore these statements in release
builds.

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]
 
Will it? I mean, the optimiser in the compiler in release mode will say that
the procedure call is empty, and has no effect, therefore the call is
altogether elliminated...


That's not exactly true; the Debug.Write statement will be compiled and
called in both Debug and Release builds. The difference is that in Release
builds the function won't actually do anything as TraceInternal class won't
have any listeners registered.

This means you will still have a (slight) overhead in Release builds
whenever Debug.Write is called.

Eric Newton said:
Also, if you use the Debug statements "Debug.WriteLine", "Debug.*", the c#
and vb.net compilers automatically ignore these statements in release
builds.

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]
 
Ed Courtenay said:
That's not exactly true; the Debug.Write statement will be compiled and
called in both Debug and Release builds.

No it won't. Here's a sample program:

using System;
using System.Diagnostics;

class Test
{
static void Main(string[] args)
{
Debug.WriteLine ("Foo");
}
}

Compiled with

csc Test.cs

the IL for Main is:

..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 1 (0x1)
.maxstack 0
IL_0000: ret
} // end of method Test::Main


Compiled with

csc /D:DEBUG Test.cs

the IL is:

..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 11 (0xb)
.maxstack 1
IL_0000: ldstr "Foo"
IL_0005: call void [System]System.Diagnostics.Debug::WriteLine
(string)
IL_000a: ret
} // end of method Test::Main


Note the lack of a call in the version where the DEBUG symbol isn't
defined.
 
My apologies - from looking through the Debug class with Lutz Roeder's
excellent Reflector, it seems that methods like Debug.Write are marked with
a [Conditional("DEBUG")] attribute. I guess that if the compiler sees this
attribute on a method it drops it from the IL when compiled without the
DEBUG flag.

Is this correct?

Jon Skeet said:
Ed Courtenay said:
That's not exactly true; the Debug.Write statement will be compiled and
called in both Debug and Release builds.

No it won't. Here's a sample program:

using System;
using System.Diagnostics;

class Test
{
static void Main(string[] args)
{
Debug.WriteLine ("Foo");
}
}

Compiled with

csc Test.cs

the IL for Main is:

.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 1 (0x1)
.maxstack 0
IL_0000: ret
} // end of method Test::Main


Compiled with

csc /D:DEBUG Test.cs

the IL is:

.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 11 (0xb)
.maxstack 1
IL_0000: ldstr "Foo"
IL_0005: call void [System]System.Diagnostics.Debug::WriteLine
(string)
IL_000a: ret
} // end of method Test::Main


Note the lack of a call in the version where the DEBUG symbol isn't
defined.
 
Ed Courtenay said:
My apologies - from looking through the Debug class with Lutz Roeder's
excellent Reflector, it seems that methods like Debug.Write are marked with
a [Conditional("DEBUG")] attribute. I guess that if the compiler sees this
attribute on a method it drops it from the IL when compiled without the
DEBUG flag.

Is this correct?

Yes, that's exactly correct.
 
Whew, I was nervous when Daniel had said that the call was still being
made... thinking about all the Debug.WriteLine's that I tend to do...!

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Jon Skeet said:
Ed Courtenay said:
My apologies - from looking through the Debug class with Lutz Roeder's
excellent Reflector, it seems that methods like Debug.Write are marked with
a [Conditional("DEBUG")] attribute. I guess that if the compiler sees this
attribute on a method it drops it from the IL when compiled without the
DEBUG flag.

Is this correct?

Yes, that's exactly correct.
 
I never said such a thing, 'twas not me sir. ;o)

I'm in the same prediciment with calls to the debugger framework i've got
setup.


Whew, I was nervous when Daniel had said that the call was still being
made... thinking about all the Debug.WriteLine's that I tend to do...!

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Jon Skeet said:
Ed Courtenay said:
My apologies - from looking through the Debug class with Lutz Roeder's
excellent Reflector, it seems that methods like Debug.Write are marked with
a [Conditional("DEBUG")] attribute. I guess that if the compiler sees this
attribute on a method it drops it from the IL when compiled without the
DEBUG flag.

Is this correct?

Yes, that's exactly correct.
 
Back
Top