ChatGPT解决这个技术问题 Extra ChatGPT

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

Wikipedia says .NET framework for C# 6.0 is 4.6.

This question (and Visual Studio 2015 CTP test) says CLR version is 4.0.30319.0.

This MSDN page says that .NET 4, 4.5, 4.5.2 uses CLR 4. There isn't any information about .NET 4.6.

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?

.Net versions 2.0 - 3.5 use CLR v2.0. Newer versions use CLR v4.0.
keep in mind that, optimize wise: you are adding a separate if-null-check for each cat like this
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

i
i3arnon

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.


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.
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 I can't think of another one other than the IFormattable string interpolation.
I guess we can say all syntactic sugars can be used and targeted to older framework versions. is it ok to say that?
@mkb yes. Syntactic sugar is exactly those features that only rely on compiler capabilities and not framework ones.
B
Bishnu Rawal

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.


C
Community

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.


o
oobe

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

S
Suryakant Soni

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.