275

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?

Community
  • 1
  • 1
MajesticRa
  • 13,770
  • 12
  • 63
  • 77
  • 7
    .Net versions 2.0 - 3.5 use CLR v2.0. Newer versions use CLR v4.0. – i3arnon Mar 08 '15 at 00:43
  • keep in mind that, optimize wise: you are adding a separate if-null-check for each cat like this – Terence Jul 14 '16 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 '16 at 12:50

5 Answers5

287

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.

i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • 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.](https://gist.github.com/svick/e5b30f73146a501cc64f) – svick Mar 09 '15 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 03 '15 at 10:37
  • 1
    @Rubenisme I can't think of another one other than the `IFormattable` string interpolation. – i3arnon Sep 09 '15 at 09:16
  • 2
    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 '16 at 11:02
  • 4
    @mkb yes. Syntactic sugar is exactly those features that only rely on compiler capabilities and not framework ones. – i3arnon Jun 15 '16 at 11:17
  • What about if you are working with an old web site project which does not need compilation (it is dynamically compiled). What about it you had an old windows 2003 machine that only supports up to framework 4.0 – Chris Mullins Nov 10 '16 at 19:41
  • @ChrisMullins what about it? – i3arnon Nov 12 '16 at 08:19
  • What about if.... Haha :) I think probably it will not work though. I don't have time right now to test. – Chris Mullins Nov 14 '16 at 17:00
  • @ChrisMullins If you use the new compiler to compile new code targeting .NET 4.0 using C# 6.0 features that don't require newer versions of .NET it will work, even for old web sites. If you use other features, or compile using an old compiler, you obviously won't be able to use these features. – i3arnon Nov 16 '16 at 13:00
  • I don't think the comment about 4.6 is correct. I know at least 4.5 works with String Interpolation. – Rick Strahl Dec 27 '16 at 09:33
  • @RickStrahl which comment? – i3arnon Dec 30 '16 at 09:16
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.

Bishnu Rawal
  • 1,387
  • 16
  • 33
Lex Li
  • 60,503
  • 9
  • 116
  • 147
29

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.

Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
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
oobe
  • 458
  • 3
  • 7
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.