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?.Friend?.Mew() ?? "Null");
        Console.WriteLine(cat?.Friend?.Friend?.TailLength ?? 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?

All Answers
  • Translate

    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.

    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}";

    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.

  • Translate

    Just want to focus on how to understand Wikipedia and other links.

    When Wikipedia says C# 6.0 is with .NET Framework 4.6, it simply means the production version of the compiler (msc.exe) will be part of the .NET Framework 4.6 release. Via multi-targeting, such compilers can support lower versions of .NET Framework releases. Of course, since Roslyn became an open source project, the compiler is now completely an individual component.

    When something refers the CLR version of 4.0.30319(.0), it actually can be .NET Framework 4.* (4.0, 4.0.*, 4.5, 4.5.*, 4.6, 4.6.*), as they all implement the CLR version 4 specification. Not to mention Xamarin/Mono also implement the same CLR specification.

    The MSDN page is not yet fully updated, but some page already has .NET Framework 4.6 listed in Version Information section.

    In all, language specs (as well as C# compiler), CLR specs, and .NET Framework releases are not tightly coupled with each other. It does give developers enough flexibility to utilize new compilers to target older CLRs and .NET Frameworks.

  • Translate

    Yes, you can use newer compilers for older frameworks and get access to the new compiler features (as long as those features do not require new types introduced in .NET 4.6).

    Other examples of this are methods with default parameters was introduced with C# 4.0 (.NET 4.0) but you are able to use them in .NET 2.0 (C# 2.0) and .NET 3.5 (C# 3.0) projects.

    You are also able to use Extension Methods (introduced in C# 3.0) in .NET 2.0 or .NET 3.0 if you do one small workaround to make the compiler happy so it can find a attribute that was introduced in .NET 3.5.

  • Translate

    If you are using building ascripts remember to change path to new builder:

    set CPATH=C:\Program Files (x86)\MSBuild\14.0\Bin


    set CPATH=C:\Program Files (x86)\MSBuild\14.0\Bin
    call nuget_restore.bat
    "%CPATH%\msbuild" YourSolution.sln /t:Rebuild /p:Configuration=Release /fileLogger /flp:logfile=JustErrors.log;errorsonly /verbosity:minimal
    if %errorlevel% neq 0 goto ERROR
    REM call deploy Release  //Things like deploy files..
    goto END
           echo ERROR: %errorlevel%

  • Translate

    Answer by @oobe is actually importatnt. I could build my solution through a batch file only after using the MSBuild.exe from C:\Program Files (x86)\MSBuild\14.0\Bin.