Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 490
MasterMastic Avatar asked Oct 28 '12 07:10

MasterMastic


People also ask

Does C have do-while?

The C do while statement creates a structured loop that executes as long as a specified condition is true at the end of each pass through the loop.

What is a do while loop in C?

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

What is the main cause of hep C?

Hepatitis C is a liver infection caused by the hepatitis C virus (HCV). Hepatitis C is spread through contact with blood from an infected person. Today, most people become infected with the hepatitis C virus by sharing needles or other equipment used to prepare and inject drugs.

Does hep C go away?

Overview. Hepatitis C virus (HCV) causes both acute and chronic infection. Acute HCV infections are usually asymptomatic and most do not lead to a life-threatening disease. Around 30% (15–45%) of infected persons spontaneously clear the virus within 6 months of infection without any treatment.


3 Answers

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

like image 133
Michal Klouda Avatar answered Oct 24 '22 00:10

Michal Klouda


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
like image 27
timfoden Avatar answered Oct 24 '22 02:10

timfoden


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

Check IF here

like image 38
Pradip Avatar answered Oct 24 '22 00:10

Pradip