可选参数code编译在.NET 3.5。为什么?可选、参数、code、NET

2023-09-04 22:57:04 作者:彼岸花〝谁的忧伤悄悄绽放

这一块code编译一个框架3.5的项目在VS 2010中确定(我三重检查了)

This piece of code compiles OK in VS 2010 in a framework 3.5 project (I triple checked that)

    public LoggingClient(string uri = "net.msmq://localhost/logging"){...}

为什么呢?我什么也看不到在C#4规格(doc版本),第21.1节,是说,这应该是向后兼容。它怎么说,我没有得到任何编译错误?这是否会在某些情况下静默失败?

Why? I see nothing in the C# 4 spec (doc version), section 21.1, that says this should be backwardly compatible. How is it that I get no compilation error? Will this fail silently in some circumstances?

推荐答案

项目+属性,生成选项卡,向下滚动,高级。您可以更改语言版本为C#3.0,如果你preFER保持源$ C ​​$ C兼容性。

Project + Properties, Build tab, scroll down, Advanced. You can change the Language Version to "C# 3.0" if you prefer to maintain source code compatibility.

但是,是的,你使用的是C#4.0编译器在VS2010,无论你使用的目标.NET版本。编译器,IL的输出,并没有改变在.NET 4.0中。不,你不能使用的动态的,它需要一个.NET 4.0只支持组件(Microsoft.CSharp.dll)

But yes, you are using the C# 4.0 compiler in VS2010, regardless of the target .NET version you use. The output of the compiler, IL, hasn't changed in .NET 4.0. No, you can't use dynamic, it requires a .NET 4.0 only support assembly (Microsoft.CSharp.dll)

 
精彩推荐
图片推荐