What are the pros and cons of using a parameter?

Anyone can specify the pros and cons of the out parameter. When it is preferable to use the out parameter instead of returning a value.

+6
c #
source share
4 answers

I would suggest taking a look at TryParse methods on built-in types such as int. The return value is a bool to indicate success, while the value is returned via the out parameter. This construct allows you to use this method in a loop construct where another type of return value will / may make it a little more complicated.

With further reflection, one con may be a tendency to simply continue to add parameters to the method instead of properly encapsulating the logic.

+4
source share
Options

Out allows you to efficiently return multiple values ​​from a method, and it is usually preferable to return an arbitrary structure or tuple that contains multiple values.

It can be argued that it is easier to overlook the possible side effects of a function that uses the out parameter, since it departs from the traditional model of "multiple parameters, one return value." But I honestly believe that the out keyword related to the post-processing of the method makes the programmer’s intent completely clear.

+6
source share

As you said, without a parameter, you can return only one value, from the keyword keyword you can "return" more than one value

0
source share

In C #, you cannot return multiple variables, so you can complete the task using the out parameter if you do not want to go through the class (return a class with these several variables).

0
source share

All Articles