Coder Perfect

Does C# 6.0 work for .NET 4.0?


I made a sample project with C#6.0 features like null propagation and properties initialization, set the target version to.NET 4.0, and it worked.

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 this imply that I can use C# 6.0 capabilities in my.NET 4.0-targeted software? Are there any restrictions or disadvantages?

Asked by MajesticRa

Solution #1

Yes, indeed (mostly). The new Roslyn compiler is required for C# 6.0, however the new compiler can also compile for 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}";

Only the latest framework version adds System, thus you’ll need.Net 4.6 to use it with IFormattable. FormattableString:

int i = 3;
IFormattable s = $"{i}";

The situations you cited don’t require the framework’s types to work. As a result, for older framework versions, the compiler is fully capable of implementing these functionalities.

Answered by i3arnon

Solution #2

I’d like to concentrate on how to comprehend Wikipedia and other connections.

When Wikipedia states C# 6.0 is included with.NET Framework 4.6, it just means that the production version of the compiler (msc.exe) will be included. These compilers can handle lower versions of the.NET Framework thanks to multi-targeting. Of course, given that Roslyn is an open source project, the compiler is now a standalone component.

Because they all implement the CLR version 4 specification, when something refers to the CLR version of 4.0.30319(.0), it might be.NET Framework 4.* (4.0, 4.0.*, 4.5, 4.5.*, 4.6, 4.6.*). Not to mention that Xamarin/Mono follow the same CLR guidelines.

Although the MSDN page is not yet entirely updated, the.NET Framework 4.6 is already featured in the Version Information section of some pages.

Overall, language specifications (as well as the C# compiler), CLR specifications, and.NET Framework updates are not tightly connected. It does, however, allow developers to use modern compilers to target older CLRs and.NET Frameworks.

Answered by Lex Li

Solution #3

You can utilize newer compilers for older frameworks and gain access to new compiler features by doing so (as long as those features do not require new types introduced in .NET 4.6).

Methods with default arguments, for example, were introduced with C# 4.0 (.NET 4.0), but they can be used 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.

Answered by Scott Chamberlain

Solution #4

Remember to modify the path to the new builder if you’re using creating ascripts:

C:Program Files (x86) set CPATH 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%


Answered by oobe

Solution #5

@oobe’s response is extremely important. Only after using MSBuild.exe from C:Program Files (x86)MSBuild14.0Bin was I able to build my solution using a batch file.

Answered by Suryakant Soni

Post is based on