I created a project in Visual Studio 2017 RC to check whether I can use new C# 7.0 language features in a .NET Framework 4.5 project. It seems to me that after referencing System.ValueTuple NuGet, new tuples are working fine. Is there anything else I should think about, or is this going to work?
After checking System.ValueTuple NuGet dependencies, it looks like .NET Framework 4.0 is not supported. Is this the case, or is there some way to make the new language work in this runtime also?
Let's go through the features new in C# 7.0:
Tuples: The
System.ValueTuplepackage has a version for theportable-net40+sl4+win8+wp8profile. That means it is usable on .Net 4.0. (Not sure why dependencies list only .Net 4.5.)If you wanted to use tuples on even lower versions of .Net, it should still work, as long as you add the code for
ValueTupleand related types to your project.outvariables, pattern matching, local functions, more expression-bodied members,throwexpressions, numeric literal syntax improvements: All these features are just syntax sugar, so they don't need any new capabilities from the framework.reflocals and returns: This feature exposes in C# what the framework supported since the start, so no changes in the framework are needed here either.Generalized async return types: To use this feature, you need a type that has the
AsyncMethodBuilderattribute, which is in theSystem.Threading.Tasks.Extensionspackage (along with one such type,ValueTask<T>). This package is only supported on .Net 4.5, so for .Net 4.0, you would need to compile the relevant types yourself. (Usingawaiton .Net 4.0 also requiresMicrosoft.Bcl.Async, but that's nothing new.)
To sum up: All of C# 7.0 should work on .Net 4.5 after installing the required packages and most of it should work on .Net 4.0 too.
Running a C# 7 compiled application on .NET 4.5 should be fine at this moment, but note that running ASP.NET applications that use ASP.NET Dynamic Compilation won't work on .NET 4.5 because the C# 7.0 compiler requires .NET 4.6 to run.
Source: https://github.com/dotnet/roslyn/issues/17908:
The C# 7.0 compiler (2.0 and higher) requires .NET 4.6 to run
The information on https://www.nuget.org/packages/Microsoft.Net.Compilers/2.0.1 (about supporting .NET 4.5) seems to be outdated.
来源:https://stackoverflow.com/questions/42482520/does-c-sharp-7-0-work-for-net-4-5