问题
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