在 VS2015 中以 .Net 4.5 为目标,但我可以使用 C# 6 功能吗?
Target .Net 4.5 in VS2015 but I am allowed to use C# 6 features?
我正在使用 VS2015 开发一个针对 .Net Framework 4.5 的项目
在构建设置中,语言版本设置为 'default'
据我所知,C# 6 仅添加到 .Net 4.6 中,但我 AM 允许(至少,代码编译和运行)使用 string interpolation
功能,我读过的是 C# 6 功能。
现在我很困惑:我现在是为 C# 6 / .Net 4.6 还是为 .Net 4.5 编译(我该如何检查?)
编辑
我在评论中看到 C# 6 语法与 .NET Framework 版本无关。
我从这个答案 (What are the correct version numbers for C#?) 中得到了灵感,其中说 'C# 6.0 released with .NET 4.6 and VS2015 (July 2015).' 所以我明白 C# 6(以某种方式)耦合到 .NET 4.6
字符串插值等 C# 6 功能是 编译器 功能,而不是运行时 (CLR) 功能。因此,只要您的编译器支持 C# 6,无论您构建的 .NET 版本是什么,您都可以使用它们。
在 Visual Studio 2015 中,您可以在 Properties
=> Build tab
=> Advanced button
=> Language Version
中控制目标语言版本
一般情况下,新版本的C# 编译器会与新版本的.Net Framework 同时发布。但是 C# 编译器不依赖于您使用的框架的版本,而是依赖于存在的特定类型和成员。这些类型包含在新框架中,但它们也可以来自其他地方。
例如,这就是为什么您可以在 .Net 4.0 上使用 C# 5.0 async
-await
,使用 the Microsoft.Bcl.Async
package.
特别针对C# 6.0,基本字符串插值不需要任何新的类型或成员,因此不需要新的框架版本。此代码:
string s = $"pie is {3.14}";
Console.WriteLine(s);
编译为:
string s = string.Format("pie is {0}", 3.14);
Console.WriteLine(s);
在 .Net 4.5 上运行良好。
另一方面,字符串插值的一个高级特性是插值后的字符串可以转换为IFormattable
或FormattableString
。例如,这段代码:
IFormattable formattable = $"pie is {3.14}";
Console.WriteLine(formattable.ToString(null, CultureInfo.InvariantCulture));
Console.WriteLine(formattable.ToString(null, new CultureInfo("cs-CZ")));
编译为:
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")));
这在 .Net 4.6 上编译良好,但在 .Net 4.5 上失败:
error CS0518: Predefined type 'System.Runtime.CompilerServices.FormattableStringFactory' is not defined or imported
但您可以通过包含以下代码使其编译:
namespace System.Runtime.CompilerServices
{
class FormattableStringFactory
{
public static IFormattable Create(string format, params object[] args) => null;
}
}
当然,使用这个虚拟实现,下面的代码将抛出 NullReferenceException
.
您实际上可以通过引用 the unofficial StringInterpolationBridge
package.
使其工作
我正在使用 VS2015 开发一个针对 .Net Framework 4.5 的项目
在构建设置中,语言版本设置为 'default'
据我所知,C# 6 仅添加到 .Net 4.6 中,但我 AM 允许(至少,代码编译和运行)使用 string interpolation
功能,我读过的是 C# 6 功能。
现在我很困惑:我现在是为 C# 6 / .Net 4.6 还是为 .Net 4.5 编译(我该如何检查?)
编辑
我在评论中看到 C# 6 语法与 .NET Framework 版本无关。 我从这个答案 (What are the correct version numbers for C#?) 中得到了灵感,其中说 'C# 6.0 released with .NET 4.6 and VS2015 (July 2015).' 所以我明白 C# 6(以某种方式)耦合到 .NET 4.6
字符串插值等 C# 6 功能是 编译器 功能,而不是运行时 (CLR) 功能。因此,只要您的编译器支持 C# 6,无论您构建的 .NET 版本是什么,您都可以使用它们。
在 Visual Studio 2015 中,您可以在 Properties
=> Build tab
=> Advanced button
=> Language Version
中控制目标语言版本
一般情况下,新版本的C# 编译器会与新版本的.Net Framework 同时发布。但是 C# 编译器不依赖于您使用的框架的版本,而是依赖于存在的特定类型和成员。这些类型包含在新框架中,但它们也可以来自其他地方。
例如,这就是为什么您可以在 .Net 4.0 上使用 C# 5.0 async
-await
,使用 the Microsoft.Bcl.Async
package.
特别针对C# 6.0,基本字符串插值不需要任何新的类型或成员,因此不需要新的框架版本。此代码:
string s = $"pie is {3.14}";
Console.WriteLine(s);
编译为:
string s = string.Format("pie is {0}", 3.14);
Console.WriteLine(s);
在 .Net 4.5 上运行良好。
另一方面,字符串插值的一个高级特性是插值后的字符串可以转换为IFormattable
或FormattableString
。例如,这段代码:
IFormattable formattable = $"pie is {3.14}";
Console.WriteLine(formattable.ToString(null, CultureInfo.InvariantCulture));
Console.WriteLine(formattable.ToString(null, new CultureInfo("cs-CZ")));
编译为:
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")));
这在 .Net 4.6 上编译良好,但在 .Net 4.5 上失败:
error CS0518: Predefined type 'System.Runtime.CompilerServices.FormattableStringFactory' is not defined or imported
但您可以通过包含以下代码使其编译:
namespace System.Runtime.CompilerServices
{
class FormattableStringFactory
{
public static IFormattable Create(string format, params object[] args) => null;
}
}
当然,使用这个虚拟实现,下面的代码将抛出 NullReferenceException
.
您实际上可以通过引用 the unofficial StringInterpolationBridge
package.