C#7.0是否适用于.NET 4.5?

编程入门 行业动态 更新时间:2024-10-28 20:27:36
本文介绍了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?

推荐答案

让我们通过 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.

通用异步返回类型:要使用此功能,您需要具有 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?

本文发布于:2023-11-04 10:42:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557700.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!