Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Like other languages in the C-family, C# supports a set of 'preprocessor' directives, most notably #define, #if and #endif (technically, csc.exe does not literally have a preprocessor as these symbols are resolved at the lexical analysis phase, but no need to split hairs…).
The #define directive allows you to set up custom symbols which control code compilation. Be very aware that unlike C(++), C#'s #define does not allow you to create macro-like code. Once a symbol is defined, the #if and #endif maybe used to test for said symbol. By way of a common example:
#define DEBUG
using System;
public class MyClass
{
public static void Main()
{
#if DEBUG
Console.WriteLine("DEBUG symbol is defined!");
#endif
}
}
When you use the #define directive, the symbol is only realized within the defining file. However if you wish to define project wide symbols, simply access your project's property page and navigate to the "Configuration Properties | Build" node and edit the "Conditional Compilation Constants" edit field. Finally, if you wish to disable a constant for a given file, you may make use of the #undef symbol.
Tip from Andrew Troelsen
Posted by: Duncan Mackenzie, MSDN
This post applies to Visual C# .NET 2002/2003/2005
Comments
- Anonymous
October 20, 2004
This is a great feature of C#, that work well in ASP.NET also. - Anonymous
October 21, 2004
If you are real-geek and miss C++ macros - you can use C++ proprocessor "cl.exe -E" to preprocess C# files.
For examples and if you would like Microsoft to support this natively - visit http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=FDBK16094
The only problem I see - it's includes. You can not reuse the same classes/interfaces in more that two files that will be compiled into one assembly. But 'partial' keyword can allow to do some magic.
It looks like I've "invented" C#++ language ;-)
Do you see any major problems with using "cl -E" for C# files ? If you will select names for defines carefully - then you will not have any. - Anonymous
November 03, 2004
Hi,
I'm a computer enginner of Genoa.
I'm working on a C# parser and I have a problem with project wide symbols. How can I obtain information about symbol which are defined and not defined in a project?
Thanks.
Antonio. - Anonymous
December 27, 2004
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/] - Anonymous
September 17, 2008
PingBack from http://blog.f9group.com/technology/c/leverage-the-c-preprocessor/