Does C# 6.0 work for .NET 4.0?
Asked Answered
M

5

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?

Mantilla answered 8/3, 2015 at 0:22 Comment(3)
.Net versions 2.0 - 3.5 use CLR v2.0. Newer versions use CLR v4.0.Pentagram
keep in mind that, optimize wise: you are adding a separate if-null-check for each cat like thisAmitosis
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! :DPerrotta
P
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.

Pentagram answered 8/3, 2015 at 0:29 Comment(11)
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.Flute
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?Catalase
@Catalase I can't think of another one other than the IFormattable string interpolation.Pentagram
I guess we can say all syntactic sugars can be used and targeted to older framework versions. is it ok to say that?Dolli
@Dolli yes. Syntactic sugar is exactly those features that only rely on compiler capabilities and not framework ones.Pentagram
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.0Mercurio
@ChrisMullins what about it?Pentagram
What about if.... Haha :) I think probably it will not work though. I don't have time right now to test.Mercurio
@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.Pentagram
I don't think the comment about 4.6 is correct. I know at least 4.5 works with String Interpolation.Clench
@RickStrahl which comment?Pentagram
S
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.

Singles answered 8/3, 2015 at 4:5 Comment(0)
S
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.

Saldana answered 8/3, 2015 at 0:29 Comment(0)
G
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
Gothard answered 3/1, 2018 at 10:8 Comment(0)
U
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.

Undeniable answered 19/6, 2018 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.