Tuesday, May 28, 2013

OUT vs REF Parameters

OUT and REF parameters are almost similar, both are used to return additional values from a function to the code which is calling the function. However there are some difference between the two, the following are the some of the key differences between the OUT and REF parameters.

Out parameters are declared by adding the keyword out in front of the parameter.


Ref parameters are declared by adding the keyword ref in front of the parameter.

The variable passes as OUT parameter should be declared but need not be initialized in the calling code.

The variable passes as REF parameter should be declared and initialized in the calling code, if the variable in not initialized in the calling code then a compiler error will be thrown.

OUT parameters can be used only as Output parameter i.e their value can only be set in the function and returned back to the calling code.

REF parameters can be used as both Input and Output parameters i.e the value of the parameter can be set in the calling code and sent to the actual function as Input and the value can be re-assigned and sent back to the calling function as Output.


OUT parameters must be set in the function before the function execution is completed.

REF parameters may or may not be re-set in the function because they are already declared and initialized in the calling code.

OUT Parameter declaration example
public int GetEmployeeDetails(out string empName)


REF Parameter declaration example
public int GetEmployeeDetails(ref string empName)

Search Flipkart Products:
Flipkart.com

No comments: