Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

Does C# 7.0 work for .NET 4.5?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let's go through the features new in C# 7.0:

  • Tuples: The System.ValueTuple package has a version for the portable-net40+sl4+win8+wp8 profile. 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 ValueTuple and related types to your project.

  • out variables, pattern matching, local functions, more expression-bodied members, throw expressions, numeric literal syntax improvements: All these features are just syntax sugar, so they don't need any new capabilities from the framework.

  • ref locals 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 AsyncMethodBuilder attribute, which is in the System.Threading.Tasks.Extensions package (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. (Using await on .Net 4.0 also requires Microsoft.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...