简体   繁体   English

在VS2015中定位.Net 4.5但我可以使用C#6功能吗?

[英]Target .Net 4.5 in VS2015 but I am allowed to use C# 6 features?

I am working in VS2015 on a project which targets .Net framework 4.5 我正在VS2015中开展一个针对.Net framework 4.5的项目

In the Build settings the language version is set to 'default' 在构建设置中,语言版本设置为“默认”

As I've read, the C# 6 is only added in .Net 4.6, but I AM allowed (at least, the code compiles and runs) to use the string interpolation feature, which I've read is a C# 6 feature. 正如我读出,C#6中的.Net 4.6只添加,但允许的(至少,代码编译和运行)使用string interpolation功能,我读过是C#6的特征。

Now I am confused: am I now compiling for C# 6 / .Net 4.6 or for .Net 4.5 (and how could I check that?) 现在我很困惑:我现在正在编译C#6 / .Net 4.6或.Net 4.5(我怎么能检查?)

EDIT 编辑

In the comments I see that C# 6 syntax has nothing to do with the .NET framework version. 在评论中我看到C#6语法与.NET框架版本无关。 I got the idea from this answer ( What are the correct version numbers for C#? ) where is said 'C# 6.0 released with .NET 4.6 and VS2015 (July 2015).' 我从这个答案中得到了这个想法( C#的正确版本号是什么? )其中说的是用.NET 4.6和VS2015(2015年7月)发布的“C#6.0”。 so I understood that C# 6 is (somehow) coupled to .NET 4.6 所以我理解C#6(以某种方式)与.NET 4.6相关联

C# 6 features such as string interpolation are compiler features, not runtime (CLR) features. C#6等字符串插值功能是编译器功能,而不是运行时(CLR)功能。 Therefore, you can use them regardless of what version of .NET you are building against, as long as your compiler supports C# 6. 因此,只要您的编译器支持C#6,您就可以使用它们而不管您构建的.NET版本。

In Visual Studio 2015 you can control which version of the language you are targeting in Properties => Build tab => Advanced button => Language Version 在Visual Studio 2015中,您可以在Properties => Build tab => Advanced button => Language Version控制您所定位的Language Version

Generally, new versions of the C# compiler are released at the same time as new versions of the .Net Framework. 通常,新版本的C#编译器与新版本的.Net Framework同时发布。 But the C# compiler does not depend on the version of the Framework you're using, instead it depends on specific types and members being present. 但是C#编译器不依赖于您正在使用的Framework版本,而是依赖于特定类型和存在的成员。 Those types are included in the new framework, but they can also come from somewhere else. 这些类型包含在新框架中,但它们也可以来自其他地方。

For example, this is why you can use C# 5.0 async - await on .Net 4.0, using the Microsoft.Bcl.Async package . 例如,这就是为什么你可以使用C#5.0 async - await .Net 4.0,使用Microsoft.Bcl.Async

Specifically for C# 6.0, basic string interpolation does not require any new types or members, so it doesn't need new framework version. 特别是对于C#6.0,基本字符串插值不需要任何新类型或成员,因此它不需要新的框架版本。 This code: 这段代码:

string s = $"pie is {3.14}";
Console.WriteLine(s);

Compiles to: 编译为:

string s = string.Format("pie is {0}", 3.14);
Console.WriteLine(s);

Which works just fine on .Net 4.5. 哪个在.Net 4.5上运行得很好。

On the other hand, one advanced feature of string interpolation is that the interpolated string can be converted to IFormattable or FormattableString . 另一方面,字符串插值的一个高级功能是插值字符串可以转换为IFormattableFormattableString For example, this code: 例如,这段代码:

IFormattable formattable = $"pie is {3.14}";
Console.WriteLine(formattable.ToString(null, CultureInfo.InvariantCulture));
Console.WriteLine(formattable.ToString(null, new CultureInfo("cs-CZ")));

Compiles to: 编译为:

IFormattable formattable = FormattableStringFactory.Create("pie is {0}", 3.14);
Console.WriteLine(formattable.ToString(null, CultureInfo.InvariantCulture));
Console.WriteLine(formattable.ToString(null, new CultureInfo("cs-CZ")));

This compiles fine on .Net 4.6, but fails on .Net 4.5 with: 这在.Net 4.6上编译很好,但在.Net 4.5上失败了:

error CS0518: Predefined type 'System.Runtime.CompilerServices.FormattableStringFactory' is not defined or imported 错误CS0518:未定义或导入预定义类型“System.Runtime.CompilerServices.FormattableStringFactory”

But you can make it compile by including the following code: 但您可以通过包含以下代码使其编译:

namespace System.Runtime.CompilerServices
{
    class FormattableStringFactory
    {
        public static IFormattable Create(string format, params object[] args) => null;
    }
}

Of course, with this dummy implementation, the following code will throw a NullReferenceException . 当然,使用这个虚拟实现,以下代码将抛出NullReferenceException

You can actually make it work by referencing the unofficial StringInterpolationBridge package . 实际上,您可以通过引用非官方的StringInterpolationBridge来使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM