[C#] User defined debug functions
ixilom

To my dismay C# doesn't support macros (Yeah, I didn't notice that until now).
But, is there a way to write functions that are "stripped" away when you do a release build?
Right now I have to do:

#if DEGUG
Tools.Logger.WriteLine("something very interesting.");
#endif

And the entire Logger class is surrounded with #if #endif.

I know typing #if DEBUG and #endif is just a few keystrokes, but it does clutter the source quite a bit if you have a whole bunch of them :-/

Essentially, what I'd like to do is something like the System.Diagnostics.Debug class ... is that even possible ???

X-G

Can't you just make the interior of the WriteLine function conditional? Surely the compiler will optimize away the empty function body and call to nothing.

EDIT: Also, there is apparently a function attribute called Conditional you can use: [Conditional("DEBUG")]

ixilom

Ah, cheers m8!

My Logger.cs now looks something like:

#SelectExpand
1#if DEBUG 2using System.Diagnostics; 3 4namespace xxx.Tools 5{ 6 7 public static class Logger 8 { 9 [Conditional("DEBUG")] 10 public static void WriteLine(string line) 11 { 12// stuff 13 } 14 } 15 16} 17#endif

And I can just use:

Tools.Logger.WriteLine("Thank you X-G :D");

Thats exactly what what I wanted 8-)
Cookie for you.

X-G

Not bad for someone who doesn't know C#.

ixilom

I bet your google/msdn-fu is better than mine ;)
I blame the lack of sleep :-X

[Edit]
I actually had to remove the #if DEBUG and #endif from Logger.cs.
The compiler would choke on not finding my Logger class when building a release build for some reason.

james_lohr

If you're coming from a C/C++ background, then in terms of language facilities, you can expect C# to have it, and, if not, for a cleaner alternative to exist.

Neil Walker

I suspect XG got his information from here, which explains everything really:

http://msdn.microsoft.com/en-us/library/4xssyw96.aspx

and you should think yourself lucky you aren't coding in archaic Java, there is no concept of attributes, conditional compilation, etc.

ixilom

Yup, I'm coming from C/C++ background and I have found C# much cleaner, no doubt there.

Java? Wouldn't touch it with a long stick to be honest.

Now, that I've had sleep and read once again on conditional attribute, I still have a question :P
Not that it really matters, but I'm still curious about whether the compiler would be smart enough to "strip" my Logger class all together as its only function (WriteLine) has been removed in a release build?
If it doesn't, I'd have a empty static class that does nothing but waste a few bytes of memory ???;D

Kibiz0r
ixilom said:

I'm still curious about whether the compiler would be smart enough to "strip" my Logger class all together

Maybe if it's internal instead of public. Executables are still assemblies, so they can be referenced by other assemblies. Obviously, it's not going to delete a type that you might want to access from another assembly.

At any rate, I wouldn't worry about it. I don't think it matters.

Neil Walker

ildasm is your friend. I'd do it for you, but I can't be bothered. Just check the code generated after a compilation, it's fairly easy to read.

ixilom
Kibiz0r said:

Maybe if it's internal instead of public. Executables are still assemblies, so they can be referenced by other assemblies. Obviously, it's not going to delete a type that you might want to access from another assembly.

Ah yes, would be bad to remove Logger in case some other assembly tries to access it.
Actually I don't think WriteLine is removed either because of the same reason, but rather just the calls to it.

Thread #601270. Printed from Allegro.cc