C# FAQ: Does CSharp have macros or a preprocessor

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

Does C# have macros or a preprocessor?

C# does not have macros. And, strictly speaking, C# does not have a preprocessor either. However, C# does have conditional compilation symbols which are used to guide compilation. These symbols can be defined either in compiler parameters or in the source code. The "preprocessing" directives in C# are so-called because of the preprocessors of its ancestors—C and C++. The following driectives are found in the ECMA specification:

Directive Usage
#define, #undef Define and undefine conditional compilation symbols.
#if, #elif, #else, #endif Skip sections of source code conditionally.
#line Control line numbers emitted with errors and warnings.
#error, #warning Issue errors and warnings.
#region, #endregion Mark sections of source code explicitly.

Refer to section 9.5 of the ECMA specification for further information on the pre-processing directives.

Also, the Conditional attribute of a method can be used to effect conditional compilation. Thus, calls to the method will be compiled only if the appropriate symbol is defined. For further information on the Conditional attribute, refer to section 24.4.2 of the ECMA specifcation.

See also


Personal tools