Sponsored Ad

Thursday, October 22, 2009

C# Define

The /define option has the same effect as using a #define preprocessor directive in your source file. A symbol remains defined until an #undef directive in the source file removes the definition or the compiler reaches the finish of the file.

You can use symbols created by this option with #if, #else, #elif, and #endif to compile source files conditionally.

The C# #define does not permit a symbol to be given a value, as in languages such as C++. For example, #define cannot be used to generate a macro or to define a constant. If you require to define a constant, use an enum variable. If you require to generate a C++ style macro, consider alternatives such as generics. Since macros are notoriously error-prone, C# disallows their use but provides safer alternatives.

For information on how to set this compiler option programmatically, see DefineConsta

Example:

// preprocessor_define.cs
// compile with: /define:xx
// or uncomment the next line
// #define xx
using System;
public class Test
{
    public static void Main()
    {
        #if (xx)
            Console.WriteLine("xx defined");
        #else
            Console.WriteLine("xx not defined");
        #endif
    }
}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers