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

I am studying another user's code, and there is a method like this:

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

Why would anyone use these attributes instead

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

Is there a difference in behavior, etc.?

+6
source share
1 answer

If they have not used C # 4, for example? I believe that the second version will be compiled into the first version ...

(I compiled them and executed them through ILDASM - there were no significant differences.)

+9
source

Source: https://habr.com/ru/post/912066/


All Articles