请问C#6.0工作.NET 4.0? [英] Does C# 6.0 work for .NET 4.0?

查看:358
本文介绍了请问C#6.0工作.NET 4.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个示例项目,用C#6.0的好东西 - 空传播和属性初始化作为一个例子,设定的目标版本的.NET 4.0和它的作品...

I created a sample project, with C#6.0 goodies - null propagation and properties initialization as an example, set target version .NET 4.0 and it... works.

public class Cat
{
    public int TailLength { get; set; } = 4;

    public Cat Friend { get; set; }

    public string Mew() { return "Mew!"; }
}

class Program
{
    static void Main(string[] args)
    {
        var cat = new Cat {Friend = new Cat()};
        Console.WriteLine(cat?.Friend.Mew());
        Console.WriteLine(cat?.Friend?.Friend?.Mew() ?? "Null");
        Console.WriteLine(cat?.Friend?.Friend?.TailLength ?? 0);
    }
}

  • 维基百科说 .NET框架的C#6.0 4.6。
  • 这个问题(和<一href="http://en.wikipedia.org/wiki/Microsoft_Visual_Studio#Visual_Studio_2015">Visual Studio 2015 CTP测试)说CLR版本是4.0.30319.0。
  • 此MSDN页面说,.NET 4,4.5,4.5.2使用CLR 4.没有任何有关.NET 4.6。
    • Wikipedia says .NET framework for C# 6.0 is 4.6.
    • This question (and Visual Studio 2015 CTP test) says CLR version is 4.0.30319.0.
    • This MSDN page says that .NET 4, 4.5, 4.5.2 uses CLR 4. There isn't any information about .NET 4.6.
    • 这是否意味着我可以使用C#6.0的功能为我的软件,面向.NET 4.0?是否有任何限制或缺点?

      Does it mean that I can use C# 6.0 features for my software that targets .NET 4.0? Are there any limitations or drawbacks?

      推荐答案

      是(主要)。 C#6.0需要新的罗斯林的编译器,但新的编译器可以针对老年framework版本。这仅限于新的功能,不需要从框架支撑

      Yes (mostly). C# 6.0 requires the new Roslyn compiler, but the new compiler can compile targeting older framework versions. That's only limited to new features that don't require support from the framework.

      例如,当你可以使用字符串插值功能在C#6.0与早期版本的.Net的(因为它导致调用的String.Format ):

      For example, while you can use the string interpolation feature in C# 6.0 with earlier versions of .Net (as it results in a call to string.Format):

      int i = 3;
      string s = $"{i}";
      

      您需要的.Net 4.6使用它的 IFormattable ,因为只有新的框架版本增加了 System.FormattableString

      You need .Net 4.6 to use it with IFormattable as only the new framework version adds System.FormattableString:

      int i = 3;
      IFormattable s = $"{i}";
      

      您提到不从架构需要类型的工作情况。所以编译器是完全能够支持这些功能的旧框架版本。

      The cases you mentioned don't need types from the framework to work. So the compiler is fully capable of supporting these features for old framework versions.

      这篇关于请问C#6.0工作.NET 4.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆