Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the .Net framework guidelines recommend that you don't use ref/out arguments?

Apparently, they're "confusing". Is that seriously the reason? Can you think of any others?

like image 811
TraumaPony Avatar asked Oct 18 '08 06:10

TraumaPony


2 Answers

Have you seen how many developers don't really understand ref/out?

I use them where they're really necessary, but not otherwise. They're usually only useful if you want to effectively return two or more values - in which case it's worth at least thinking about whether there's a way of making the method only do one thing instead. Sometimes using ref/out is the most appropriate approach - the various TryParse methods etc.

like image 166
Jon Skeet Avatar answered Oct 07 '22 00:10

Jon Skeet


In my opinion, they are considered a code smell because in general there is a much better option: returning an object.

If you notice, in the .NET library they are only used in some special cases, namely tryparse-like scenarios where:

  • returning a class would mean boxing a value type
  • the contract of the method requires it to be fast, and so boxing/unboxing is not a viable option.
like image 21
Sklivvz Avatar answered Oct 07 '22 00:10

Sklivvz