Is it possible to detect if the current version of C# is 6 or above with a preprocessor directive, so at compile time?
I want to do something like this:
var myVar = ...;
string name;
#if VERSION_6_OR_MORE
name = nameof(myVar);
#else
name = "myVar";
#endif
I use Visual Studio 2015 and C# 6, so I can use nameof()
. Someone else wanting to compile this code however may be using an older version, where nameof()
isn't present.
I want to use a preprocessor directive so I can keep the nameof()
in C# 6, but someone else who doesn't use that version can compile it as well.