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.
我创建了一个示例项目,其中有c# 6.0的好处——空传播和属性初始化作为示例,设置目标版本。net 4.0,然后……的工作原理。
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.
- *说c# 6.0的。net框架是4.6。
- This question (and Visual Studio 2015 CTP test) says CLR version is 4.0.30319.0.
- 这个问题(和Visual Studio 2015 CTP测试)说CLR版本是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.
- 这个MSDN页面说。net 4,4.5, 4.5.2使用clr4。没有任何关于。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?
这是否意味着我可以为我的软件使用c# 6.0的特性,目标是。net 4.0?有什么限制或缺点吗?
5 个解决方案
#1
266
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.
是的(大部分)。c# 6.0需要新的Roslyn编译器,但是新的编译器可以编译针对旧的框架版本。这仅仅局限于不需要框架支持的新特性。
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
):
例如,当您可以使用c# 6.0中的字符串插值特性时,使用。net的早期版本(因为它会调用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
:
您需要。net 4.6来使用IFormattable,因为只有新的框架版本添加了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.
您提到的案例不需要框架中的类型来工作。因此编译器完全能够支持旧框架版本的这些特性。
#2
49
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.
当*说c# 6.0和。net Framework 4.6在一起时,这仅仅意味着编译器的生产版本(msc.exe)将成为。net Framework 4.6版本的一部分。通过多目标,这样的编译器可以支持。net框架的低级版本。当然,由于Roslyn成为了一个开源项目,编译器现在完全是一个独立的组件。
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.
当某物指的是4.0.30319(.0)的CLR版本时,它实际上可以是。net Framework 4。*(4.0、4.0。*,4.5,4.5。因为他们都实现了CLR版本4规范。更不用说Xamarin/Mono也实现了相同的CLR规范。
The MSDN page is not yet fully updated, but some page already has .NET Framework 4.6 listed in Version Information section.
MSDN页面还没有完全更新,但是有些页面已经在版本信息部分列出了。net Framework 4.6。
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#编译器)、CLR规范和。net框架的发布都没有紧密耦合。它确实给了开发人员足够的灵活性来利用新的编译器来针对旧的CLRs和。net框架。
#3
27
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).
是的,您可以为旧框架使用更新的编译器并访问新的编译器特性(只要这些特性不需要. 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.
其他的例子是使用c# 4.0引入默认参数的方法。但是你可以在。NET 2.0 (c# 2.0)和。NET 3.5 (c# 3.0)项目中使用它们。
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.
你也可以在。net 2.0或。net 3.0中使用扩展方法(在c# 3.0中引入),如果你做一个小的变通,让编译器满意,这样它就可以找到。net 3.5中引入的属性。
#4
0
If you are using building ascripts remember to change path to new builder:
如果您正在使用建筑属性,请记住更改新构建器的路径:
set CPATH=C:\Program Files (x86)\MSBuild\14.0\Bin
设置CPATH = C:\程序文件(x86)\[\ 14.0 \ Bin
[Rebuild.bat]
(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
#5
0
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.
@oobe的回答实际上很重要。我可以在使用MSBuild之后才通过批处理文件构建解决方案。exe从C:\Program Files (x86)\MSBuild\14.0\Bin.
#1
266
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.
是的(大部分)。c# 6.0需要新的Roslyn编译器,但是新的编译器可以编译针对旧的框架版本。这仅仅局限于不需要框架支持的新特性。
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
):
例如,当您可以使用c# 6.0中的字符串插值特性时,使用。net的早期版本(因为它会调用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
:
您需要。net 4.6来使用IFormattable,因为只有新的框架版本添加了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.
您提到的案例不需要框架中的类型来工作。因此编译器完全能够支持旧框架版本的这些特性。
#2
49
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.
当*说c# 6.0和。net Framework 4.6在一起时,这仅仅意味着编译器的生产版本(msc.exe)将成为。net Framework 4.6版本的一部分。通过多目标,这样的编译器可以支持。net框架的低级版本。当然,由于Roslyn成为了一个开源项目,编译器现在完全是一个独立的组件。
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.
当某物指的是4.0.30319(.0)的CLR版本时,它实际上可以是。net Framework 4。*(4.0、4.0。*,4.5,4.5。因为他们都实现了CLR版本4规范。更不用说Xamarin/Mono也实现了相同的CLR规范。
The MSDN page is not yet fully updated, but some page already has .NET Framework 4.6 listed in Version Information section.
MSDN页面还没有完全更新,但是有些页面已经在版本信息部分列出了。net Framework 4.6。
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#编译器)、CLR规范和。net框架的发布都没有紧密耦合。它确实给了开发人员足够的灵活性来利用新的编译器来针对旧的CLRs和。net框架。
#3
27
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).
是的,您可以为旧框架使用更新的编译器并访问新的编译器特性(只要这些特性不需要. 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.
其他的例子是使用c# 4.0引入默认参数的方法。但是你可以在。NET 2.0 (c# 2.0)和。NET 3.5 (c# 3.0)项目中使用它们。
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.
你也可以在。net 2.0或。net 3.0中使用扩展方法(在c# 3.0中引入),如果你做一个小的变通,让编译器满意,这样它就可以找到。net 3.5中引入的属性。
#4
0
If you are using building ascripts remember to change path to new builder:
如果您正在使用建筑属性,请记住更改新构建器的路径:
set CPATH=C:\Program Files (x86)\MSBuild\14.0\Bin
设置CPATH = C:\程序文件(x86)\[\ 14.0 \ Bin
[Rebuild.bat]
(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
#5
0
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.
@oobe的回答实际上很重要。我可以在使用MSBuild之后才通过批处理文件构建解决方案。exe从C:\Program Files (x86)\MSBuild\14.0\Bin.