I'm looking for a way to implement something like this:
#if CSHARP_COMPILER_IS_FOR_CSHARP_6_OR_HIGHER
foo?.Bar();
#else
if (foo != null)
{
foo.Bar();
}
#endif
Of course, I could define a conditional compilation symbol by myself, but it isn't suitable.
Is there any built-in constant?
The questions I found are rather old. Maybe, the things were changed to the best?
The preprocessor #endif directive.
Conditional compilation provides a way of including or omitting selected lines of source code depending on the values of literals specified by the DEFINE directive. In this way, you can create multiple variants of the same program without the need to maintain separate source streams.
#elif means "else if" and establishes an if else-if compilation chain. Amongst other things, #if provides an alternative method of "commenting out" code. For example, #if 0 printf("#d", total); #endif. Here, the compiler will ignore printf("#d", total); #ifdef and #ifndef.
Conditional compilation is the process of selecting which code to compile and which code to not compile similar to the #if / #else / #endif in C and C++. Any statement that is not compiled in still must be syntactically correct. Conditional compilation involves condition checks that are evaluable at compile time.
No, from what I know they didn't change anything :-)
You could perhaps do a little magic inside the csproj to define the constants... but it is complex...
There is no property that specifies directly the version of the CSC... There is a property ($(LangVersion)
) that specifies the version of the language standard required... but it is normally set to default
so "the maximum the compiler supports"...
Or you could look at the path of the CSC compiler... It is stored in the CscToolPath
or, if that is empty, in the MsBuildToolsPath
. From there perhaps you can discern the version of the CSC.
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