276

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

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?

3
  • 7
    .Net versions 2.0 - 3.5 use CLR v2.0. Newer versions use CLR v4.0.
    – i3arnon
    Mar 8, 2015 at 0:43
  • keep in mind that, optimize wise: you are adding a separate if-null-check for each cat like this
    – Terence
    Jul 14, 2016 at 21:06
  • 2
    My goodness. I've been developing a WCF targeting v4.6 just to be "reminded" that the production server is not to be upgraded until 2018. I thought a month's worth of work would require a few days of refactoring. Finished in five minutes.Thank you, Microsoft! :D
    – Eric Wu
    Oct 25, 2016 at 12:50

5 Answers 5

288

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.

11
  • 33
    Actually, you don't need .Net 4.6, just a few types that are added in it. You can add those to older frameworks yourself. This code works fine on .Net 4.0 and 3.5.
    – svick
    Mar 9, 2015 at 20:22
  • 10
    Is there a list somewhere to see which C# 6 features work in .NET 4.0? Another way of asking this would be: which new features require support from the framework and which ones don't?
    – Rubenisme
    Sep 3, 2015 at 10:37
  • 1
    @Rubenisme I can't think of another one other than the IFormattable string interpolation.
    – i3arnon
    Sep 9, 2015 at 9:16
  • 3
    I guess we can say all syntactic sugars can be used and targeted to older framework versions. is it ok to say that?
    – mkb
    Jun 15, 2016 at 11:02
  • 5
    @mkb yes. Syntactic sugar is exactly those features that only rely on compiler capabilities and not framework ones.
    – i3arnon
    Jun 15, 2016 at 11:17
52

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.

30

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.

1

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

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

[Rebuild.bat]

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

:ERROR
       echo ERROR: %errorlevel%
       pause

:END
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.