Does C# 6.0 work for .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);
    }
}
  • 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.

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?

5 Answers
5

Leave a Comment