Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.?

like image 550
Sunny Milenov Avatar asked Mar 30 '12 16:03

Sunny Milenov


1 Answers

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.)

like image 162
Jon Skeet Avatar answered Sep 18 '22 11:09

Jon Skeet