C#7.0是否适用于.NET 4.5? [英] Does C# 7.0 work for .NET 4.5?

查看:255
本文介绍了C#7.0是否适用于.NET 4.5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2017 RC中创建了一个项目,以检查是否可以在.NET Framework 4.5项目中使用新的C#7.0语言功能。在我看来,在引用 System.ValueTuple NuGet之后,新的元组工作正常。还有什么我应该考虑的,还是行得通的?

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?

检查 System.ValueTuple NuGet之后依赖关系,看起来好像不支持.NET Framework 4.0。是这种情况,还是有某种方法也可以使新语言在此运行时中运行?

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?

推荐答案

让我们通过< a href = https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/csharp-7 rel = noreferrer> C#7.0中的新增功能:


  • Tuples: System.ValueTuple 软件包具有 portable-net40 + sl4 + win8 + wp8 配置文件的版本。这意味着它在.Net 4.0上可用。 (不确定为什么依赖项仅列出.Net 4.5。)

  • 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.)

如果您想在甚至更低版本的.Net上使用元组,只要您仍然可以使用元组添加 ValueTuple 和相关类型到您的项目。

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 变量,模式匹配,局部函数,更多表达式体成员, throw 表达式,数字文字语法改进:所有这些功能都只是语法糖,因此它们不需要使用任何新功能

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 本地并返回:此功能在C#中公开了自开始以来所支持的框架,因此也不需要在框架中进行任何更改。

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.

通用异步返回类型:要使用此功能,您需要具有<$ c的类型$ c> AsyncMethodBuilder 属性,该属性位于 System.Threading.Tasks.Extensions (还有一个这样的类型, ValueTask< T> )。该软件包仅在.Net 4.5上受支持,因此对于.Net 4.0,您需要编译您自己的相关类型。 (在.Net 4.0上使用 await 还需要 Microsoft.Bcl.Async ,但这并不新鲜。)

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 awaiton .Net 4.0 also requires Microsoft.Bcl.Async, but that's nothing new.)

总结:安装所需的软件包后,所有C#7.0都应在.Net 4.5上运行,并且大多数应在.Net 4.0也是如此。

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.

这篇关于C#7.0是否适用于.NET 4.5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆