Preprocessor definitions in C#
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4

Thread: Preprocessor definitions in C#

  1. #1
    Join Date
    Dec 2009

    Question Preprocessor definitions in C#

    Is it possible to use preprocessor definitions in C# like we do in C and C++?

    #define DEBUG
    // ...
    #ifdef DEBUG
    Console.WriteLine("This is a debug message.");
    #endif // #ifdef DEBUG

  2. #2
    Join Date
    Jun 2008

    Re: Preprocessor definitions in C#

    Yes, you can define them in Properties > Build. The "DEBUG" symbol is already defined for you.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too -

  3. #3
    Join Date
    Aug 2005

    Re: Preprocessor definitions in C#

    For your sample, the only thing that changes is "#ifdef DEBUG" becomes "#if DEBUG".

    Note that C# #define's are strictly 'flag-type' #defines - you can't specify replacements or macros as you can in C/C++.
    David Anton
    Convert between VB, C#, C++, & Java
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  4. #4
    Join Date
    Mar 2004
    Prague, Czech Republic, EU

    Re: Preprocessor definitions in C#

    For conditional debugging it is possible, but nothing like C++ macros are available in C#.

    Symbol checked in #if condition can be also defined when invoking compiler from command line with /define: e.g. csc /define: DEBUG.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

HTML5 Development Center