Remember Top 3 important points working with out and ref in C#

 

Most of the developers while working with "out" and "ref" keyword in C# get confused especially who are new to programming or the one who are weak in programming fundamentals when to use what? This confusion arises as there is a very thin between them but it has huge difference from output prospect.

So we will start learning it right below and its practical scenario will make you learn with ease.

Point 1 By default - one way "Caller" to "Callee" passing variable using "by val"

By default in C# program when variables are passed they are "by val" and not by reference from one function or method. With value of the variables going to other method or function and variable value gets changed with its operation but if you check outside variable it is still unaffected. This is because of variable passing as "by val" means that copy of variable is first created on memory and then variable is passed to other method or function.

Let us explain with below example where we have two methods one is the Main method which is "Caller" while other method is with name AddFunction and it is "Callee". "Caller" has a variable with name "OutVar" with value "30" while "Callee" has variable name "InVar". It does addition with 20 of the passing variable "OutVar" when "Caller" calls "Callee". So now "InVar" with addition of "30 + 20"becomes "50 and now if you check value of the outside variable "OutVar" it is still "20". This was only possible because by default value of variable passing of one method to other method is "by val".

One liner definition

They are keyword of C# and with the use of "out" and "ref" keyword in your program proves to be very helpful in passing variables to functions and methods by reference.

This means that it will not create copy variable value on memory and will use that memory location only which will change or keep it unchanged. So with this way of working it will be very much helpful in various situation.

Point 2: "Ref" Two way calling "Caller" to "Callee" & "Callee" to "Caller" using reference

Using "ref" keyword in our existing program method "Caller" will call method "Callee" using reference. And it will make a two way calling by doing so it will not only pass variable "OutVar" value "30" to "InVar" but also it will pass reference.

So now the outcome of "Invar" will be 30+20 which is "50" will be assigned to "OutVar" and gets it displayed to the console prompt. This states that two calling is being successfully done.

Below image depicts the flow of program and shows the sequence of the variable going from "Caller" to "Callee" and back from "Callee" to "Caller".

So in the situation where we want pass variable value from one method to other method and get those changed variable from that method back and use it then we will use "ref" keyword.

Point 3: "Out" - One way calling from "Callee" to "Caller" using reference

And the third point to remember is that "out" keyword when used it only does calling of "Callee" to "Caller" only.

It will be used in the scenario where we only want variable can pass value from "Callee"to "Caller" in other words we can say even if we try to pass value of a variable "OutVar" from "Caller" to "Callee" it will accept it but before we use variable "InVar" of "Callee" method it has to be initialized to "0" until that we cannot use it as program will show error after you "Build" the solution.

Once we do that then variable can be used to call "Caller" and use new value of variable in that Main method. As "out" keyword also uses reference way due that value of "OutVar" will change from "30" to "20".

Hope that all three points explained and demonstrated above is clear and differences in their working is understood. So from now on there will be no longer confusion between "ref" and "out" keyword while working with C#.

We will come up with many such small but interesting topics on C# programming see you in article.

Before ending this article also see the following project based practical series video which will make a strong programmer. This video is startup video of C# 100 hrs series for more videos of this series, visit on www.learncsharptutorial.com

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Dec
07
2016

Type Safe in C#.NET step by step using example

CLICK TO READ FULL ARTICLE

Views - 213 |Category - C#

Dec
06
2016

App Domain in C#.NET Step by Step using example

CLICK TO READ FULL ARTICLE

Views - 366 |Category - C#

Dec
01
2016

Delay signing in C# with example

CLICK TO READ FULL ARTICLE

Views - 194 |Category - C#

Nov
28
2016

Tuples in c# example step by step

CLICK TO READ FULL ARTICLE

Views - 354 |Category - C#

Nov
28
2016

C# Anonymous Types vs Tuples with an example

CLICK TO READ FULL ARTICLE

Views - 454 |Category - C#

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

i1368