gpt4 book ai didi

c# - 在 C#6.0 中编码但针对 .NET 2.0 进行编译的缺点

转载 作者:行者123 更新时间:2023-11-30 20:20:30 26 4
gpt4 key购买 nike

今天我突然想到这个问题。

C#6.0 或与此相关的任何其他版本都引入了很酷的新功能,使代码更容易编写。

但是假设我有一个运行 WindowsXP 的客户端,它不支持 .NET 4.6。

用 C#6.0(或将来的任何最新 C# 版本)编写我的代码但针对 .NET 2.0 框架(或不同于 C# 的其他一些 .NET 版本)进行编译是否有任何缺点?它发布的版本)

编辑

对于那些不知道的人,您可以通过项目属性 -> 应用程序选项卡 -> 目标框架来定位 .NET Framework

您可以通过项目属性 -> 构建选项卡 -> 高级 -> 语言版本来定位 C# 语言版本。

EDIT2 - 在 .NET 2.0 上编译并在 WindowsXP 上运行的 C#6.0 代码

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();
}

最佳答案

Are they any downsides to writing my code in C#6.0 (or whatever the latest C# version is in the future) but compiling it against the .NET 2.0 framework (or some other version of .NET which is different than the C# version that it was released with)

只是会有一些你不能使用的功能:

  • LINQ,除非您提供自己的实现
  • 表达树
  • 扩展方法,除非您添加自己的属性
  • FormattableString 用于内插字符串(次要;您仍然可以将内插字符串用作字符串)
  • 动态输入
  • 一般方差
  • 除非您添加自己的属性,否则来电者信息
  • Async/await(不确定提供您自己的此实现的可行性......非常重要的工作)

其他功能,如 lambda 表达式、表达式体成员、匿名类型等应该可以正常工作。

关于c# - 在 C#6.0 中编码但针对 .NET 2.0 进行编译的缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36454287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com