C# 您可以在面向 .NET 3.5 的代码中使用可选参数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1210679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 11:00:46  来源:igfitidea点击:

Can you use optional parameters in code targeting .NET 3.5?

c#visual-studio-2010.net-4.0

提问by Charles Graham

I'm looking to write a library that uses the new optional parameters feature of C# 4.0, but I want to target it to the 3.5 version of the framework. Is this possible?

我希望编写一个使用 C# 4.0 的新可选参数功能的库,但我想将它定位到 3.5 版本的框架。这可能吗?

Are optional parameters syntactic sugar in the same way that extension methods are?

可选参数的语法糖和扩展方法一样吗?

采纳答案by Bernard Vander Beken

With VS2010 RC, I was able to create a .NET 3.5 application that uses optional parameters in C#.

使用 VS2010 RC,我能够创建一个使用 C# 中的可选参数的 .NET 3.5 应用程序。

So yes, it's all about syntactic sugar.

所以是的,这都是关于语法糖的。

回答by Timothy Carter

I don't have VS2010 installed here to check, but I believe this would be purely a language feature, and therefore should be usable regardless of the framework being targeted.

我没有在这里安装 VS2010 进行检查,但我相信这纯粹是一种语言功能,因此无论目标框架如何,都应该可用。

Edit: Looking at this link(and a few others), it appears that optional parameters compile to method arguments with an [opt] attribute in the il. I don't know if this parameter existed in previous versions of the clr, but still my guess would be that it does.

编辑:查看此链接(以及其他一些链接),似乎可选参数编​​译为 il 中带有 [opt] 属性的方法参数。我不知道这个参数是否存在于以前版本的 clr 中,但我仍然猜测它确实存在。

回答by Joe Chung

VB.NET has optional parameters if you want to use optional parameters in .NET 3.5.

如果您想在 .NET 3.5 中使用可选参数,则 VB.NET 具有可选参数。

回答by demius

VS 2010 supports optional parameters in C# for .NET 3.5 solutions. One caveat however, is that the command-line compiler will report errors.

VS 2010 支持 C# 中的可选参数,用于 .NET 3.5 解决方案。然而,一个警告是命令行编译器会报告错误。

So, if you have automated builds in running, - using NANT or something else using the command-line compiler - your builds will fail.

因此,如果您有正在运行的自动构建——使用 NANT 或使用命令行编译器的其他东西——您的构建将失败。

回答by Lee Richardson

Like Jon Skeet I was getting "Feature 'optional parameter' cannot be used because it is not part of the 3.0 C# language specification". However in the RTM version of Visual Studio you can select Language Version to "default" in Project Properties->Build->Advanced. That got it to work for me.

像 Jon Skeet 一样,我收到“无法使用功能‘可选参数’,因为它不是 3.0 C# 语言规范的一部分”。但是,在 Visual Studio 的 RTM 版本中,您可以在“项目属性”->“构建”->“高级”中选择“语言版本”为“默认”。这让它为我工作。

回答by Frans Harinck

You can use optional and named parameters in a targeted framework of a previous version as long the assemblies are build within a development environment for v4 (f.e. VS2010). But you should be aware of runtime compiling or publishing websites in this case the compiler of the targeted framework will be used. And because the compilers of the older frameworks doesn't understand the syntax of optional and named params it will results in compiler errors.

只要程序集是在 v4 (fe VS2010) 的开发环境中构建的,您就可以在先前版本的目标框架中使用可选参数和命名参数。但是您应该注意运行时编译或发布网站,在这种情况下将使用目标框架的编译器。并且由于旧框架的编译器不理解可选参数和命名参数的语法,因此会导致编译器错误。

回答by devi

Go to project properties -> Build Tab -> click button 'Advanced' -> set language version to 'default' -> save -> be happy ;)

转到项目属性-> 构建选项卡-> 单击按钮“高级”-> 将语言版本设置为“默认”-> 保存-> 开心;)