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. 🕗
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With