MasterMastic
MasterMastic

Reputation: 21286

Does C# Have Predefined Symbols?

In C++ I have this: http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx .

So I can write code that will run only when I'm debugging, or only for specific platforms (PowerPC, MIPS, Power Macintosh etc. it's not supported anymore but it's a very good example). You could also switch like that between 32bit and 64bit systems (of course that's only useful when you're releasing 2 different builds, each with its own processor architecture. in C# it's a rare need but nevertheless, it is there).

So my question is if something like that exists in C#. I realize there are no macros, but there are however symbols (even with the same #define keyword. so it kind of make sense -- for me at least -- that it should exist.

Upvotes: 11

Views: 6819

Answers (3)

timfoden
timfoden

Reputation: 443

Although there don't appear to be any targetting different operating systems, there are some for the various different .NET frameworks:

  • .NET Framework
    • NET20, NET35, NET40, NET45, NET451, NET452, NET46, NET461, NET462, NET47, NET471, NET472
  • .NET Standard
    • NETSTANDARD1_0, NETSTANDARD1_1, NETSTANDARD1_2, NETSTANDARD1_3, NETSTANDARD1_4, NETSTANDARD1_5, NETSTANDARD1_6, NETSTANDARD2_0
  • .NET Core
    • NETCOREAPP1_0, NETCOREAPP1_1, NETCOREAPP2_0, NETCOREAPP2_1

Upvotes: 9

Michal Klouda
Michal Klouda

Reputation: 14521

In .NET there are Pre-processing directives. To include some block of code only when debugging, you could do:

#define Debug      // Debugging on

class PurchaseTransaction
{
   void Commit() {
      #if Debug
         CheckConsistency();
      #else
         /* Do something else
      #endif
   }
}

If you go to Project settings in Visual Studio, check the Build tab. There you can define Debug and Trace constants for selected configuration (checked by default for Debug build). You can also define additional conditional compilation symbols.

enter image description here

Upvotes: 9

Pradip
Pradip

Reputation: 1537

MSDN documentation does not list any pre-defined names. It would seem that all need to come from #define and /define.

Check IF here

Upvotes: 5

Related Questions