Visual Studio Tip: Compilation Symbols

There are times when we need to use different code statements for different build configurations – this means that some code will not exist in assemblies that are built in certain build configurations.

Compilation symbols come to help in this case. You can set a symbol that will exist in  a specific build configuration and then use it inside your code files to write or exclude code when this project is built using this build configuration.

The most familiar case of this scenario is debug and release build configurations. Actually, the debug build configurations comes with a compilation symbol by default named DEBUG. By using it you can write code that will run only when you compile your project in debug mode (show extra message boxes, write more logs, etc.).

Creating Compilation Symbols

  1. In Visual Studio, go to Project-> <project name> Properties…
  2. The project properties view will open. Click on “Build” on the left.
  3. The Build settings will open on the right. The first field there, with the label “Conditional compilation symbols” is the one you need.
  4. Add there the name or names (semi-colon separated) of the compilation symbols you want to define. Pay attention that the naming convention for these symbols is all uppercase.
  5. Save

You’re done. You can start using the compilation symbols you have just  defined. The next screenshot contains a sample project with two symbols defined – SHAY and FRIEDMAN:

IronShay | Compilation Symbols

Using Compilation Symbols

In order to use the compilation symbols you have defined, you need to use the special compilation conditions. If these conditions are not met, the code within the condition will not be compiled at all.

The condition starts with #if <condition> and ends with #endif. For example, the next code will be compiled only when SHAY is defined (which is in debug build configuration):

#if SHAY

Console.WriteLine("SHAY is defined! woot!");

#endif

You can also write more complicated conditions like !SHAY for all build configuration without SHAY defined. You can add conditions too – SHAY && FRIEDMAN.

Well, that’s about it!
Hope it helps,
Shay.

kick it on DotNetKicks.com


Comments

October 15. 2009 06:39 PM

trackback

Visual Studio Tip: Compilation Symbols

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

January 19. 2010 05:32 PM

breville juice fountain elite

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

breville juice fountain elite

December 3. 2011 12:56 PM

hotels in victoria

This is the first time i am reading your post and admire that you posted article which gives users lot of information regarding particular topic. Thanks for this share.

hotels in victoria

December 3. 2011 02:28 PM

hotels in valencia

This blog contains a lot of informative data and news which i first time read from here. Thanks for writing about this important topic.

hotels in valencia

January 17. 2012 05:01 AM

friv

Cool post ! Many thanks for, posting on my blog man. I shall message you all over again! I didnt know that.

friv

January 17. 2012 05:02 AM

y8

This is a great article tackling some of the most difficult issues we have to face in the work place.
Adapting is one of the main things I think is important and so is tackling hard work head on and not waiting

y8

January 17. 2012 05:02 AM

y8

Excellent post. I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

y8

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Subscribe Subscribe

That's Me!

Hi! I'm Shay Friedman
I'm Shay Friedman - a Visual C#/IronRuby MVP, a consultant and instructor of .NET technologies, author, speaker and new technologies freak
More about me

Contact Me

> Contact page
> Twitter: @ironshay
> LinkedIn profile

Search

Hosted By

I'm hosting this site on Arvixe and I'm very happy with it.
If you're looking for ASP.NET hosting, I highly recommend it
(and if you order from this link I also get some beer money!)
Web Hosting