Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can compilation of C# code be made to require a given language or compiler version?

Tags:

c#

How can a C# program detect it is being compiled under a version of C# that does not contain support for the language features used in that program?

The C# compiler will reject the program, and produce some error message, on encountering the features of the language it does not support. This does not address the issue, which is to state that the program is being compiled with too old a version of the C# compiler, or a C# compiler that does not support the required version of C#

Ideally, it would be as simple as

#if CS_VERSION < 3
#error CSharp 3 or later is required
#end
like image 647
grrussel Avatar asked Jun 05 '09 12:06

grrussel


1 Answers

I don't believe you can do that with a C# file, but if you're using MSBuild then the project/solution tools version number can stop it from being built with an older version of MSBuild.

Can you give the exact context of this? One "human" solution rather than a technical one might be to try compiling the code with all the "old" versions, and create a document with: "If you get an error like this it probably means you're using the wrong version..."

Another option you might want to consider to make that even simpler is to have a single "required features" file. This would be unused by your main app, but ask users to compile that first. If it works, the rest of your build should work. If it doesn't, it's due to using the wrong version. That's likely to produce a smaller range of errors from different versions (in particular it doesn't have the problem that the compiler could list errors from different files in a random order).

like image 129
Jon Skeet Avatar answered Sep 24 '22 00:09

Jon Skeet