1

Is it possible to check for the C# version from code in maybe the environment settings?

I would like to use #if preprocessor directive to selectively choose what version to compile for. The reason being is that sometimes we will ship source code examples and if the end user is using an older IDE which might not support for example c#6.0, the compilation fails and causes loss of time from both sides. I would like to resolve this and provide just one source which does selective compilation based on what might be supported.

Based on some reading online, I can't seemt to find any resource on this? Has anyone else tried to do something similar?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
CJC
  • 795
  • 8
  • 25
  • 2
    *"we will ship source code examples and if the end user is using an older IDE"* - can't you simply add instruction file (readme.txt) to example instead of making code more spagetty-alike with inactive (due to failed `#if`) code blocks? – Sinatr Feb 08 '17 at 10:43
  • https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx to obtain .Net Framework – Dmitry Bychenko Feb 08 '17 at 10:43
  • C# doesn't have the same level of flexibility in pre-processor work as C++, etc do. Things that are `#define`d can't have values and the compiler doesn't include any by default that you could use. – TZHX Feb 08 '17 at 10:44
  • 2
    @Dmitry I don't see how that comment is useful, as it's telling how to determine available .NET framework versions, not the currently supported version of C#. – Jonathon Reinhart Feb 08 '17 at 10:45
  • Moved to comment: Are you talking about Framework version? (In that case use: Environment.Version) https://msdn.microsoft.com/en-us/library/system.environment.version(v=vs.110).aspx – Ori Nachum Feb 08 '17 at 10:46
  • 1
    If you want to support older IDEs, simply don't use C# 6.0 code. There is no compiler variable that indicates the C# version. – Sefe Feb 08 '17 at 10:48
  • @OriNachum No I am talking about C# version. – CJC Feb 08 '17 at 10:48
  • 1
    I´d also suggest to avoid relying on the C#-version as it produces unreadable code. Stay with two different versions of your assembly. – MakePeaceGreatAgain Feb 08 '17 at 10:48
  • @Sinatr Yes but in production, the software engineers may be tied to using a particular VS version. But the example source code that we ship need to cater to different parties. I just wanted to avoid having to provide two different source codes for different VS versions. – CJC Feb 08 '17 at 10:50
  • I suppose its not possible, and as advice from everyone, that its probably better to release different versions of the assembly. Ok thank you everyone. With proper version control, there should be no issues to support different C# versions – CJC Feb 08 '17 at 10:52

0 Answers0