【发布时间】:2016-04-06 14:34:31
【问题描述】:
今天我突然想到了这个问题。
C#6.0 或任何其他版本都引入了很酷的新功能,使代码更容易。
但是假设我有一个运行 WindowsXP 的客户端,它不支持 .NET 4.6。
在 C#6.0(或将来的最新 C# 版本)中编写我的代码但针对 .NET 2.0 框架(或与 C# 不同的其他 .NET 版本)进行编译时,它们有什么缺点吗?发布时使用的版本)
编辑
对于那些不知道的人,您可以通过项目属性 -> 应用程序选项卡 -> 目标框架来定位 .NET Framework
您可以通过项目属性 -> 构建选项卡 -> 高级 -> 语言版本来定位 C# 语言版本。
EDIT2 - C#6.0 代码在 .NET 2.0 上编译并在 WindowsXP 上运行
static void Main(string[] args)
{
try
{
StringBuilder sb = null;
string value = sb?.ToString(); // C#6 Feature. Wont throw an exception.
if(value == null)
{
Console.WriteLine("The Value is null");
}
string value2 = sb.ToString(); // Will cause an "Object Reference not set to an instance" exception
}
catch ( Exception ex) when (ex.Message.Contains("object")) // C#6.0 conditional catches
{
Console.WriteLine("Exception Caught");
}
catch(Exception ex)
{
Console.WriteLine("Other Exception");
}
Console.ReadLine();
}
【问题讨论】:
-
@Vladimir 不正确。许多 C# 6.0 功能是编译时的,而 .Net 的版本是运行时的。它只会影响您可以使用的库。
-
@Vladimir 那不是真的。我在我的问题中添加了一些 C#6.0 代码,这些代码在 .net 2.0 上编译和运行
标签: c# .net frameworks c#-6.0