Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use out parameters?

Tags:

c#

.net

out

I don't understand when an output parameter should be used, I personally wrap the result in a new type if I need to return more than one type, I find that a lot easier to work with than out.

I have seen method like this,

   public void Do(int arg1, int arg2, out int result) 

are there any cases where that actually makes sense?

how about TryParse, why not return a ParseResult type? or in the newer framework return a null-able type?

like image 762
kay.one Avatar asked Jul 23 '09 05:07

kay.one


People also ask

When should I use an out parameter in C#?

C# out parameter is used when a method returns multiple values. When a parameter passes with the Out keyword/parameter in the method, then that method works with the same variable value that is passed in the method call. If variable value changes, the method parameter value also changes.

What is the difference between ref & out parameters?

ref is used to state that the parameter passed may be modified by the method. in is used to state that the parameter passed cannot be modified by the method. out is used to state that the parameter passed must be modified by the method.

Do you need to declare an out variable before you use it?

Variables passed as out arguments do not have to be initialized before being passed in a method call. However, the called method is required to assign a value before the method returns.

Should we initialize an out parameter before a method returns?

Generally speaking, out parameters must be initialized before the called method returns control to the caller.


1 Answers

Out is good when you have a TryNNN function and it's clear that the out-parameter will always be set even if the function does not succeed. This allows you rely on the fact that the local variable you declare will be set rather than having to place checks later in your code against null. (A comment below indicates that the parameter could be set to null, so you may want to verify the documentation for the function you're calling to be sure if this is the case or not.) It makes the code a little clearer and easier to read. Another case is when you need to return some data and a status on the condition of the method like:

public bool DoSomething(int arg1, out string result); 

In this case the return can indicate if the function succeeded and the result is stored in the out parameter. Admittedly, this example is contrived because you can design a way where the function simply returns a string, but you get the idea.

A disadvantage is that you have to declare a local variable to use them:

string result; if (DoSomething(5, out result))     UpdateWithResult(result); 

Instead of:

UpdateWithResult(DoSomething(5)); 

However, that may not even be a disadvantage, it depends on the design you're going for. In the case of DateTime, both means (Parse and TryParse) are provided.

like image 154
jasonh Avatar answered Oct 10 '22 02:10

jasonh