Why are some C # 4 features allowed even when targeting 3.5?

We upgraded to VS 2010, but we have not upgraded to .NET 4.0 yet. All of our projects are still clearly focused on 3.5.

Today, the developer is tested in code that looks like this:

delegate T Generator<out T>(); 

As far as I know, โ€œin Tโ€ and โ€œout Tโ€ are C # 4.0 features. According to our build server on which .NET 4.0 is not installed, I'm right. Registration completed. But why the hell is he building his car? Why does VS just ignore the target structure for the project?

Other C # 4.0 features, such as a dynamic keyword, are not built on the developer's machine.

+4
source share
4 answers

To avoid this, use Project + Properties, Build tab, scroll down, Advanced, Language Version = "C # 3.0"

+1
source

The C # specification is developed separately from the CLR. This means that if the C # function can be converted to code for an earlier version of .NET, it can still be used when targeting an earlier version of the framework.

+6
source

These are language features that require a C # 4 compiler, but not for the .NET 4.0 platform. So, while all the machines that will be built have a C # 4 compiler, you should be able to use these new features without requiring additional updates for your users.

+4
source

Some of the new language features are independent of the new CLR version. For example, you can use some C # 3.0 features when targeting .NET 2.0 (extension methods, lambda expressions, object initializers, or even query syntax if you provide an implementation for Linq operators, such as LinqBridge). These functions depend only on the compiler, and not on the CLR (in .NET 3.5 the CLR is the same as in .NET 2.0).

Likewise, covariance and contravariance are independent of the .NET 4.0 CLR, they are an exclusively language feature, so you can use in / out modifiers when setting up .NET 3.5. Dynamic typing, on the other hand, is dependent on the DLR (Dynamic Language Runtime) that ships with .NET 4.0, so you cannot use dynamic when targeting 3.5

+1
source

Source: https://habr.com/ru/post/1308691/


All Articles