Ref and Out Parameters In Methods

In previous post we learned about parameters and methods, now take a baby step further on to understand two new C# keywords in context of methods. These two keywords are ref & out. These keywords are used while passing parameters into the methods. Let's learn more about these.

ref vs out parameters in C#

Why We Need Ref & Out Keyword?

A method can return only one variable of any built-in data type or custom type. But what if we want to return multiple values from a method? Here ref & out comes into picture but till we explain further understand that ref & out are almost similar in usage. We can use these keywords in parameters to get the outcome from method through the parameters.

Sounds confusing?? Consider below example you will feel better.

decimal percentage;
int marks;
bool hasPassed;
percentage = GetResult(marks, out hasPassed);

public decimal GetResult(int marks, out bool hasPassed)
    int totalMarks = 500;
    decimal percentage = (marks/totalMarks)*100; 
    if(percentage > 40)
        hasPassed = true;
        hasPassed = false;
    return percentage;

//Calling of the above function
//marks = 450
//percentage = 90, hasPassed = true

In the above example we are calling GetResult method and getting two things percentage and whether the student has passed or not. So this way we are getting more than one value from a method.

You can pass any number of ref or out parameter to a method.

To accept a parameter as out parameter, we write out in front of the variable name. Like in above example method public decimal GetResult(int marks, out bool hasPassed) we have written out before bool hasPassed. Also we have to write out while passing the parameter to the method. Like in the above example for parameter hasPassed percentage = GetResult(marks, out hasPassed);

Similarly while using ref parameter we write ref in method decleration as well while calling the method. Now let's discuss the basic difference between ref vs out. Both are almost equal but thier is a slight change in their nature.

Ref Parameter

Ref parameter is used when we want the method to change the value of an existing variable. In a method, parameters are passed as value so if you assign any value to the passed on parameter inside method, the changes will not be reflected to that variable after the method is executed. To sustain the changes made inside the method we use ref type parameter.

Remember a ref type parameter should be initialized with some value before passing into the method, otherwise compiler throws an error "Use of unassigned local variable 'YourVariableName'"

Out Parameter

Out parameter is used when we want some value from the method exclusively. In this case we send a variable as a parameter and it's method's responsibility to return the value of that parameter. It is not required to assign some value to out parameter before sending to the method as in the case of ref parameter.

Remember it is mandatory for the method to assign some value to the out parameter and if your code doesn't do that then compiler throws an error "The out parameter 'YourVariableName' must be assigned to before control leaves the current method"

Have a look at our video explaining ref vs out

That's all about ref and out parameters in C# methods. Let us know if you any query or concerns. Happy Reading (refer full series here - C# Tutorial Index)


Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example