c# - When should I use out parameters? -
I do not understand that when an output parameter should be used, By wrapping the result of me if I return more than one type, I think it is very easy to work in comparison to the outside.
I have seen such a method,
give public zero (int arg1, int
how
tryParse < / Code> about, why not back
code> parscult or an empty-type type comes back in the new framework?
When you have the TryNNN
function and it is clear that the out-parameter will always be set, Even if the function is not successful, you can rely on the fact that After the local variables you declare, then your code will be set instead of checking against zero. (A comment below indicates that the parameter can be set to null
, therefore You may want to verify the document for the function that you are asking to make sure it is a case or not.) This makes the code a bit clearer and easy to read. When you need to return some data and position on the condition of this type:
public bool DoSomething (int arg1, outside string result);
In this case returns indicate that the function is successful and the result is stored in the outside parameter. Of course, this example is hypothetical because you can prepare a method where the function returns only one string
, but you get this idea.
A disadvantage is that you use a local variable:
string result; If (DoSomething (5, out of results)) UpdateWithResult (results); Instead of
:
update result (dozomething (5));
However, this may not even be a disadvantage, it depends on the design going for you. In the case of date time, both methods (pars and trippers) are provided.
Comments
Post a Comment