C# 7.0 是否适用于 .NET 4.5? [英] Does C# 7.0 work for .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 中的新特性:
元组:
System.ValueTuple
包 有一个用于portable-net40+sl4+win8+wp8
配置文件的版本.这意味着它可以在 .Net 4.0 上使用.(不知道为什么依赖项只列出 .Net 4.5.)
Tuples: The
System.ValueTuple
package has a version for theportable-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 await
on .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屋!