Preprocessor vs Conditional Compilation Attribute

Here is a little teaser program –

class Program {

static int test = 0;

    static void Main(string[] args) 

static void TestMethod(int t) { }

Like all questions, should this compile or not? The thing to note here is the difference in behavior between the Conditional compilation attribute and a preprocessor directive. The answer is in the documentation of the conditional attribute class.

Applying ConditionalAttribute to a method indicates to compilers that a call to the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with ConditionalAttribute is defined. Applying ConditionalAttribute to an attribute indicates that the attribute should not be emitted to metadata unless the conditional compilation symbol is defined. Any arguments passed to the method or attribute are still type-checked by the compiler.


UPDATE to my question in the community DL by Eric Lippert

>>So this in my understanding means that unlike a preprocessor directive, happens only during translation and that means type checking and parsing etc does happen even though the method is conditional in a sense and removed only at a later stage.

Correct. If you think about it a moment you’ll see why that has to work that way. Ask yourself a few questions about how this works:

Q: How does the compiler know to remove the method? 

A: Because the method called has the conditional attribute on it.

Q: How does the compiler know that the method called has the conditional attribute on it?

A: Because overload resolution chose that method, and the metadata associated with that method has the attribute.

Q: How does the compiler know to choose that method?

A: By examining its arguments.

Therefore the arguments must be well-defined at the point of the call, even if the call is going to be removed. In fact, the call CANNOT be removed unless the arguments are there!

>>Could someone point me to which parts of the c# spec defines this behavior?

FYI, the specification begins with a handy table of contents”, which is very useful for answering such questions. The table of contents of the specification states:

2.5.1 Conditional compilation symbols

17.4.2 The Conditional attribute

So my advice would be for you to look at sections 2.5.1 and 17.4.2 if you want the specification for conditional compilation symbols vs the conditional attribute.



Seattle Symphony – Tchaikovsky’s Winter Dreams

I have never understood why I loved music of this form. I’ve listened to this since I was like 14 but could never understand why a conductor waved the way he/she did. I did find out cause there was a “Talk” with the Maestro after the concert. I know its a new discovery and I must admit I was pleasantly surprised. Sitting in the first row helps you ever hear the every press the oboist makes.  I didn’t really know what to expect. The Benaroya Hall where we went to see the “Winter Dreams” was to say much more than perfect. It was snowing that day and I think it was the first day of snow in Seattle. First thing that surprised me was the Maestro – Carolyn Kuan as I had never seen a female conductor before and there was to top it, Oboe by Ben Hausmann.

I’m hoping to go for Handel’s Messiah on 18th. Lets see how that works out. I don’t understand why these don’t get the hype that other forms of music however odd, end up getting. The fact still puzzle’s me and I’m pretty sure that some guys thought I was nuts when I was in school and picked up 2 tapes of Mozart. It was hard to get this kind of stuff where I grew up and the lovely thing is that you can be as ignorant about the details and the history behind it but yet enjoy it simply.

Mort, Elvis, Einstein – WCF and Async

The “classic” classification problem has been around for quite while in Microsoft (and every where else) and this reference is something that you would hear quite often. You can find Nikhil Kothari’s post about Applying personas where he uses this analogy to describe types of programmers and another interesting post too The intent here is not to describe this, those other links will help for that, but to call out how sometimes some features are buried deep in the guts of WCF. Well I maybe exaggerating a bit here. Anyway, one of the challenges of being a performance developer on this team is that I rarely get to deal with simplistic scenarios. Continue reading

The EventLog and Message limits

Sometimes I have to fix certain issues in BizTalk and during one of these exercises I had to write a large message into the eventlog.OUCH! Ok now who does that ? Well I’m not at fault here but then again we did require this for some convoluted reason. Anyway there was some whacky boundary condition code. So to get a quick value just decided to flood my event log and see how much can it store.

        static void Main(string[] args)
            int limit=31000;
            int source = 10; //212 is the limit here.
                    SD.EventLog.WriteEntry(new string(‘s’,source) , new string(‘v’, limit), System.Diagnostics.EventLogEntryType.Information, 0, 0);
                catch (Win32Exception ex)

            SD.EventLog.WriteEntry(new string(‘s’, source), new string(‘v’, limit), System.Diagnostics.EventLogEntryType.Information, 0, 0);
            Console.WriteLine(“Total = ” + (source + limit));

And guess – its a magic number 31884  and not 32k. System.Diagnostics.Eventlog actually uses advapi32!ReportEvent but this seems to have a 32k limit. Anyway short answer is that its not “exactly” 32k.