What is the difference between OptionalAttribute and optional parameters in C# 4.0

孤人 提交于 2021-02-07 13:30:36

问题


I'm researching someone else's code and there is a method like this:

public SomeClass DoSomething(string param1, [Optional, DefaultParameterValue("")] string optional)

Why would someone use these attributes instead of

public SomeClass DoSomething(string param1, string optional = "")

Is there any difference in the behavior, etc.?


回答1:


If they weren't using C# 4, for example? I believe the second version will compile into exactly the first version...

(I've compiled them both and run them through ILDASM - there were no significant differences.)



来源:https://stackoverflow.com/questions/9946919/what-is-the-difference-between-optionalattribute-and-optional-parameters-in-c-sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!