Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# !Conditional attribute?

Does C# have a not Conditional (!Conditional, NotConditional, Conditional(!)) attribute?


i know C# has a Conditional attribute:

[Conditional("ShowDebugString")] public static void ShowDebugString(string s) {    ... } 

which is equivalent1 to:

public static void ShowDebugString(string s) { #if ShowDebugString    ... #endif } 

But in this case i want the inverse behavior (you have to specifically opt out):

public static void ShowDebugString(string s) { #if !RemoveSDS    ... #endif } 

Which leads me to try:

[!Conditional("RemoveSDS")] public static void ShowDebugString(string s) {    ... } 

which doesn't compile. And:

[Conditional("!RemoveSDS")] public static void ShowDebugString(string s) {    ... } 

which doesn't compile. And:

[NotConditional("RemoveSDS")] public static void ShowDebugString(string s) {    ... } 

which doesn't compile because it's only wishful thinking.

1Not true, but true enough. Don't make me bring back the Nitpicker's Corner. 🕗

like image 980
Ian Boyd Avatar asked Nov 22 '11 16:11

Ian Boyd


People also ask

What is C in simple words?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C or C++ same?

While C and C++ may sound similar, their features and usage differ. C is a procedural programming language that support objects and classes. On the other hand C++ is an enhanced version of C programming with object-oriented programming support.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

First, having the Conditional attribute is not equivalent to having #if inside the method. Consider:

ShowDebugString(MethodThatTakesAges()); 

With the real behaviour of ConditionalAttribute, MethodThatTakesAges doesn't get called - the entire call including argument evaluation is removed from the compiler.

Of course the other point is that it depends on the compile-time preprocessor symbols at the compile time of the caller, not of the method :)

But no, I don't believe there's anything which does what you want here. I've just checked the C# spec section which deals with conditional methods and conditional attribute classes, and there's nothing in there suggesting there's any such mechanism.

like image 154
Jon Skeet Avatar answered Sep 28 '22 00:09

Jon Skeet


Nope.

Instead, you can write

#if !ShowDebugString [Conditional("FALSE")] #endif 

Note that unlike [Conditional], this will be determined by the presence of the symbol in your assembly, not in your caller's assembly.

like image 30
SLaks Avatar answered Sep 28 '22 00:09

SLaks