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

Disadvantages of coding in C#6.0 but compiling against .NET 2.0

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

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

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

用 C#6.0(或将来的任何最新 C# 版本)编写我的代码但针对 .NET 2.0 框架(或不同于它发布的 C# 版本)

编辑

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

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

EDIT2 - 在 .NET 2.0 上编译的 C#6.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 表达式、表达式体成员、匿名类型等其他功能应该可以正常工作。