Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

Many time while working in our application module, we need to use “out” and “ref” parameters in various methods. Sometime you will face this question in front of interview panel too. So, what are those keywords and how they differ?

 

In this post, we will discuss the basics of C# to make you understand more about “out” and “ref” parameters which you will find useful.

 

In an interview panel, while judging the basics of C# I ask this question frequently “What is out and ref parameters and how they differ?” and maximum time I get something different answer.

 

Hence, I thought to share the same with you here and if you came to this page by just searching online, it will help you to understand the need of those parameter types in your methods.

 

What is “out” parameter?

 

The “out” parameter keyword on a method causes a it to refer the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in the variable when control passes back to the calling method.

 

In C#, a method can return only one value. If you like to return more than one value, you can use the out keyword. The out modifier return as return-by-reference. The simplest answer is that the keyword “out” is used to get the value from the method.

 

Always keep this in mind that:

    • You don't need to initialize the value in the calling function.
    • You must assign the value in the called function, otherwise the compiler will report an error.

For example just refer the below code, where the first method will result a compile time error because we did not assign any value to the out parameter that we passed to this method. In the second method, it will fill the result before exiting the method call. In the third example, it will return more than one result as output parameters.

public void CompileError(int a, int b, out int result)
{
    // this will throw compile time error as the out parameter is not 
    // assigned before function exit call.
}
 
public void Sum(int a, int b, out int result)
{
    // modify the result parameter before exit
    result = a + b;
}
 
public void Calculate(int a, int b, out int sum, out int multiply)
{
    // return multiple values using multiple out parameters
    sum = a + b;
    multiply = a * b;
}
 
 
// out parameter must be initialized before call
int result, sum, multiply;
 
Sum(5, 4, out result);
Calculate(5, 4, out sum, out multiply);
  

 

As shown in the above code snippet, always declare the out parameter before calling the respective method in your code. Once you call the method will return you the result in the out parameter.

 

What is “ref” parameter?

 

A parameter declared with a “ref” modifier is a reference parameter. Unlike a value parameter, a reference parameter will not create a new storage location instead it will represent the same storage location as the variable given as the argument in the method.

 

In C#, when you pass a value type such as int, float, double, structure etc. as an argument to the method parameter, it is passed by value. Therefore, if you modify the parameter value, it does not affect argument in the method call. But if you mark the parameter with “ref” keyword, it will reflect in the actual variable.
 
To pass by reference, use “ref” keyword. Therefore, if you modify the parameter value, it affects argument in the method call.

 

Always keep this in mind that:

    • You need to initialize the variable before you call the function.
    • It’s not mandatory to assign any value to the ref parameter in the method. If you don’t change the value, what is the need to mark it as “ref”?

For example, refer the below code snippet where the integer value will be passed as reference and inside the method the actual instance will be modified. The first call will throw compile time error because the value was not initialized before the call but the second call to this will work perfectly as we initialized the variable before passing it to the method.

public void Increment(ref int value)
{
    value++;
} 
 
int myValue;
Increment(ref myValue); // return compile error as the ref parameter was not initialized
 
int myValue = 5;
Increment(ref myValue); // will execute properly

 

 

As shown in the above code snippet, always initialize the ref parameter before calling the respective method in the code. The rest will work just like out parameter but it is not mandatory to modify the variable inside the method (If you don’t change the value, what is the need to mark it as “ref”? Right?)

 

End Note

I hope that, this will help you to clarify the difference between the “out” and “ref” parameters inside a method definition. Next time onwards you will not miss it again. Stay tuned to my next post on this series. Suggestions are always welcome for improve this post as well as I will also accept suggestions for next post.

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.