what is the pros and cons of using out parameter

前端 未结 4 1100
囚心锁ツ
囚心锁ツ 2021-01-19 10:22

Anyone can point out the pros and cons of the out parameter. When it is preferred to use out parameter rather than just to return a value.

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-19 11:11

    I would suggest taking a look at the TryParse methods on the built in types like int. The return value is a bool to indicate success while the value is returned via an out parameter. This construct makes it useful to call this method in a looping construct where another return type would/might make it a bit more complicated.

    On further reflection one con could be a tendency to just keep adding out parameters to a method instead of properly encapsulating the logic.

提交回复
热议问题