MSDN的VS2010 命名和可选参数(C#编程指南)告诉我们C#中的可选参数,显示我期望的代码:
public void ExampleMethod(int required,
string optionalstr = "default string",
int optionalint = 10)
Run Code Online (Sandbox Code Playgroud)
好的,但它也说:
您还可以使用.NET OptionalAttribute类声明可选参数.OptionalAttribute参数不需要默认值.
我阅读了MSDN的OptionalAttribute页面,并在线完成了搜索(显示很多人声称OptionalAttribute参数不能被C#使用 - 我猜这些评论是在C#4之前做出的?),但我找不到答案两个问题:
如果我使用OptionalAttribute将C#参数定义为可选: