Compare the Difference Between Similar Terms

Difference Between out and ref in C#

Key Difference – out vs ref in C#
 

C# is a modern programming language developed by Microsoft. It is used for developing various applications for desktop, web and mobile. Functions or methods is a concept in programming. That group of statements that are used to perform a specific task is known as a function or a method.  In C# programs, the execution begins from main (). It is an example of a method. When calling a method, data is passed to the method or received from the method. The method that is calling the new method is known as calling the method. The new method is known as the called method. For managing the passing values and getting back the result, C# uses parameters. They are valued parameters, output parameters and reference parameters. The value parameters are used for passing parameters into the method by value. The reference parameters are used to pass parameters to the method by reference. The output parameters are used to pass the result back from the method. In C#, out keyword is used for output parameters and ref keyword is used to reference parameters. The key difference between out and ref in C# is that, out is a keyword used to refer an output parameter that is used to pass results from the called method to calling method while ref is a keyword to refer a reference parameter that is used to pass data from calling method to called method and to receive the data from the called method to calling method.

CONTENTS

1. Overview and Key Difference
2. What is out in C#
3. What is ref in C#
4. Similarities Between out and ref in C#
5. Side by Side Comparison – out vs ref in C# in Tabular Form
6. Summary

What is out in C#?

The syntax for function or a method is as follows. A method has a number of elements such as method name, parameter list, return type and access specifier.

<access specifier> <return type> <method name> (parameter list)

{   //statements to execute

}

Each method has a unique method to make the function call. The executable statements are inside curly braces. The return type explains whether the function returns a value or not. When there is no return type, it is called void. Access specifier is used to specify the method accessibility in the application. The parameters are used to receive or pass data to the method. There can also be methods with no parameters. Parameters can be valued parameters, output parameters or reference parameters.

The output parameters are used to pass results back to the calling method. For that, the parameter should be declared with the keyword out. The output parameter does not create a new storage location. Generally, a method returns one value. But in C#, it is possible to return two values from a function using output parameters. Refer the below program.

namespace application1{

            public class Calculation{

public void display(out int a, out int b){

int value =5;

a= value;

b= value;

a = a* a;

b= b*b;

}

public static void main(String [] args){

int value1= 10, value2= 20;

Calculation  cal= new Calculation();

cal.display(out value1, out value2);

Console.ReadLine();

}

}

}

The display function is called from the main method. The value1 and value two have 10 and 20, but they are not taken into the method. The value of a is 25 and value of b is also 25 in the function. Therefore, those values are returned. When printing value1 and value2, it won’t give 10 and 20. Instead, it will print 25 and 25. Using the out keyword multiple values can be returned from the method.

What is ref in C#?

When passing parameters by value, a new storage location is created for each parameter. The actual parameters that were sent from the main program do not change. Instead, those values are copied to a separate location. Those new variable copies are called formal parameters. Refer the below code.

namespace application1{

public class Calculation{

public void swap(int x, int y){

int temp;

temp=x;

x=y;

y= temp;

}

public static void main(String [] args){

Calculation cal= new Calculation();

int p = 2;

int q = 3;

cal.swap(p,q);

Console.WriteLine(p);

Console.WriteLine(q);

Console.ReadLine();

}

}

}

According to the above program, the Calculation class has a method swap (). In the main program, an object of type Calculation is created. There are two values such as p and q. The variable p has value 2 and q variable has value 3.  Those two values are passed to the swap method. In the swap method, the value 2 is copied into variable x and value 3 is copied to variable y. Using the temp variable, the values are swapped. Back in the main program, when printing p and q, the values are not swapped. The p value is still 2 and q value is 3. Even in the swap method, values are swapped but does not reflects in the main program.

Unlike passing values as above, it is possible to pass parameters by reference. A reference variable is a reference to the memory location. There is no new memory location is created. The changes in the method reflected in the in the main program.  In C#, the reference parameters are referred using ref keyword. Refer the below program.

namespace application1{

public class Calculation{

public void swap(ref int x, ref int y){

int temp;

temp = x;

x = y;

y = temp;

}

public static void main(String [] args){

Calculation cal= new Calculation();

int p = 2;

int q = 3;

cal.swap(ref p,ref q);

Console.WriteLine(p);

Console.WriteLine(q);

Console.ReadLine();

}

}

}

According to the above program, the class Calculation has the swap method. In the main program, an object of type Calculation is created. The are two variables such as p and q. The p variable has the value 2 and q variable has the value 3. Rather than passing values, the reference to the memory locations of p and q are passed to the method. Those reference variables are referred using ref. In the swap method, rather than coping the values to a new location, the changes are made to the actual parameters. When printing the p and q values of the main program, it will give the swapped values. Now the p value is 3 and q value is 2.

What is the Similarity Between out and ref in C#?

What is the Difference Between out and ref in C#?

out vs ref in C#

The out is a keyword in C# that is used to refer an output parameter. It is used to pass the results from a called method to calling the method. The ref is a keyword in C# that is used to refer a reference parameter. It is used to pass data from calling method to called method and to return data from the called method to calling method.
Functionality
When using the keyword out, the data can be passed from calling method to the called method. But that data is discarded. It is used to pass the result to the calling method from called method. When using the keyword ref, the data is passed from calling method to the called method and the manipulated data is passed back to the calling method.
Passing of Data
When using the keyword out, the data is passed in one way that is a called method to the calling method. When using the keyword ref, the data is passed in two ways that are, from calling method to the called method and from called method to the calling method.

Summary – out vs ref in C# 

When invoking a method, it might be necessary to pass the values into the method and to get back the results from the method.  C# has different parameters to achieve that. The value parameters are used for passing parameters into the method by value. The reference parameters are used to pass parameters to the method by reference. The output parameters are used to pass the result back from the method. In C#, out keyword is used for output parameters and ref keyword is used to reference parameters. The difference between out and ref in C# is that, out is a keyword used to refer an output parameter that is used to pass results from called method to calling method while ref is a keyword to refer a reference parameter that is used to pass data from calling method to called method and to receive the data from the called method to calling method.

Download the PDF of out vs ref in C#

You can download the PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here: Difference Between Out and Ref in C#

Reference:

1.tutorialspoint.com. “C# Methods.”  The Point. Available here 
2.dnfvideo. C# Out parameters Vs REF parameters, .NET Interview Preparation videos, 28 Sept. 2015. Available here  

Image Courtesy:

1.’C sharp’By Mothmolevna – Own work, (Public Domain) via Commons Wikimedia