How can I compile C # code to require a language or compiler version?

How does a C # program detect that it is compiled under a C # version that does not contain support for the language functions used in this program?

The C # compiler will reject the program and produce some error message when faced with the capabilities of a language that it does not support. This does not concern the problem that the program is compiled with an too old 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
+5
source share
5 answers

, # , MSBuild, / MSBuild.

? "" , , , "" : " โ€‹โ€‹, , , ..."

, , , , - " ". , . , . , - . , , ( , , ).

+4

, . , generics (detects 2.0), (3.0) (4.0)

+3

; script. , :

โ†’ โ†’ โ†’ :

  • ISO-1 - # 1.2
  • ISO-2 - # 2.0

( csc /langversion)

, - , - . , ; ... test .

:

int[] arr1 = { 1, 2, 3, 4, 5 };
string[] arr2 = Array.ConvertAll(arr1, delegate (int i) {return i.ToString();});

This works in .NET 3.5 / ISO-2 (pseudo C # 2.0), but does not work in .NET 2.0 (correct C # 2.0).

+2
source

Simple: the compiler will fail and give you an error if it cannot compile the code.

+1
source

Perhaps you can read the supportedRuntime element:

  

You can then determine the desired directive based on the supported runtime using CSharpProjectConfigurationProperties3.DefineConstants .

You must create a macro in VS to use it.

0
source

All Articles