473,466 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

#ifndef _DEBUG equivalent...

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.
Nov 15 '05 #1
10 18582
#if DEBUG

// code here only runs in debug mode
#endif

"Daniel Bass" <I'm really @ sick of spam> wrote in message
news:eM**************@tk2msftngp13.phx.gbl...
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.

Nov 15 '05 #2
<"Daniel Bass" <I'm really @ sick of spam>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Daniel Bass" <I'm really @ sick of spam>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4

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" <er**@cc.ensoft-software.com> wrote in message
news:ud*************@tk2msftngp13.phx.gbl...
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Daniel Bass" <I'm really @ sick of spam>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #5
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...

"Ed Courtenay" <my***********@edcourtenay.co.uk> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...

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" <er**@cc.ensoft-software.com> wrote in message
news:ud*************@tk2msftngp13.phx.gbl...
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Daniel Bass" <I'm really @ sick of spam>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 15 '05 #6
Ed Courtenay <my***********@edcourtenay.co.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ed Courtenay <my***********@edcourtenay.co.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8
Ed Courtenay <my***********@edcourtenay.co.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ed Courtenay <my***********@edcourtenay.co.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10
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.
"Eric Newton" <er**@cc.ensoft-software.com> wrote in message
news:uF**************@TK2MSFTNGP09.phx.gbl...
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ed Courtenay <my***********@edcourtenay.co.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Evan | last post by:
Is there any standard for when to use #if !defined(SOME_INCLUSION_GUARD) versus #ifndef SOME_INCLUSION_GUARD? Aside from being able to combine multiple things into an #if, is there any...
3
by: prettysmurfed | last post by:
Hi all I have this, probably stupid question, how to avoid multiple definitions when a header file is included more than once. I thought, when you wrote the header-file and used the...
5
by: DrUg13 | last post by:
#ifndef HEADER_H #define HEADER_H blah blal #endif So this tells the compiler That if its defined do not define it again. Could someone help me understand this. Does this mean that if I...
3
by: Michael Sgier | last post by:
Hello I get the error below. But why and what does ifndef? Well what is ifndef? Many thanks and regards Michael error: syntax error before `(' token #ifndef MD2SwapInt static __inline__...
25
by: John Hanley | last post by:
I have a program where both my main.c and program.c files use the program.h file. So I #include "program.h" in both the .c files. The program.h file has #ifndef PROGRAM_H #define PROGRAM_H...
9
by: Qiao Jian | last post by:
I am new to c. Today I just read an h file within which there is statements: #ifndef _RANDOM_H #define _RANDOM_H So what is the meaning or purpose of this statement? When should I use such...
5
by: vfunc | last post by:
Despite a #ifndef I am getting a redefinition error The offending .h file looks like the following #ifndef DISTRIB_H #define DISTRIB_H double dblpi; // this is getting compiled twice ...
6
by: canoewhiteh2o | last post by:
I am converting a couple of C header files to C#. It is mainly just a bunch C structs but I am not sure how to handle the #ifdef and #ifndef in C#. For example: #ifndef DATE_TIME #define...
7
by: Studlyami | last post by:
Okay I have been wondering this for a while now. I am a little confused on the difference between these two defines #pragma once & #ifndef. From my understanding #pragma is compiler...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.