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
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")]
Ah, cheers m8!
My Logger.cs now looks something like:
And I can just use:
Tools.Logger.WriteLine("Thank you X-G :D");
Thats exactly what what I wanted
Cookie for you.
Not bad for someone who doesn't know C#.
I bet your google/msdn-fu is better than mine
I blame the lack of sleep
[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.
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.
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.
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
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
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.
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.
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.