Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional compilation depending on compiler version

Tags:

c#

c#-6.0

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?

like image 814
Dennis Avatar asked Aug 13 '15 08:08

Dennis


People also ask

Which compiler directive is used for conditional compilation?

The preprocessor #endif directive.

What is true about conditional compilation?

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.

What is conditional compilation explain with example?

#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.

What is conditional compilation in C programming?

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.


1 Answers

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.

like image 166
xanatos Avatar answered Oct 31 '22 06:10

xanatos