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

标签 c# .net frameworks c#-6.0

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

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/

相关文章:

c# - 锐化滤镜 - 无效果

c# - EPPLUS 的公式问题

c# - ICollection<T> 的简单现有实现

swift - 如何使我的公共(public) Cocoa pods 存储库在 cocoapods.org 上有文档,而不是直接重定向到我的 github 页面?

cocoa - 还有像 BWToolkit 这样的 Cocoa 组件库吗?

c# - 适当的互操作清理

c# - 如何在 LINQ 查询期间将 XML 属性(字符串)解析为(int)

c# - ajax jquery .net 结果为 "The resource cannot be found."

c# - 我如何调试 SessionStateModule/REQUEST_AQUIRE_STATE 在我的一半请求上花费 > 100 秒?

swift - 如何从 OS X 命令行工具使用 Swift 框架或库