I have 4+ experience and working as software Engineer with Hytech professionals India PVT ltd and also working as Consultant,Trainer,Blogger and Author on Microsoft technologies,Also I have Trained more than 500+ IT professionals.I have knowledge about Asp.net,C#,SQL,Project management,replication,MVC,WebApi,SSRS,PostMan,TFS,SVN,Swagger,WMI,Agile,SignalR,Angularjs,IIS configuration.

What is Ref and Out in c# ?

Today I will clarify about ref and out parameters in c#.net with example. Both ref and out parameters are use to pass argument inside a method. These ref and out parameters are helpful at whatever point your method needs to return more than one value . As to parameter you have to instate it before going to the method and out parameter you don't have to introduce before going to work.

Ref:-The ref keyword passes arguments by reference. It means any changes made to this argument in the method will be reflected in that variable when control returns to the calling method.
It must  initialized before passing to the Method. The ref keyword on a method parameter causes a method to refer to the same variable that was passed as an input parameter for the same method. If you do any changes to the variable, they will be reflected in the variable.

public static void Main() 
 int i = 5; // Variable must initialized.
 TestMethod(ref i );  

public static void TestMethod(ref int TestData) 

Out:-The out keyword passes arguments by reference. This is very similar to the ref keyword.
 It must  to be initialized before passing to Method. The out parameter can be used to return the values in the same variable passed as a parameter of the method. Any changes made to the parameter will be reflected in the variable.

public static void Main() 
 int i, j; // Variable no need to be initialized 
 TestMethod(out i, out j); 
public static int TestMethod(out int TestData1, out int TestData2) { 
 TestData1 = 100; 
 TestData2 = 200; 
 return 0; 
Difference between Ref and Out:-  
1.The  argument must be initialized first before it is passed to ref.It is not mandatory to initialize a parameterbefore it is passed to an out.
2.It is not necessary to assign or initialize the value of a parameter (which is passed by ref) before returning to the calling method.A called method is need to assign or initialize a value of a parameter (which is passed to an out) before returning to the calling method.
3.Passing a argument value by Ref is useful when the called method is also needed to modify the pass parameter.Declaring a argument to an out method is useful when multiple values need to be returned from a  method.
4.It is not mandatory to initialize a parameter value before using it in a calling method.A parameter value should be initialized within the calling method before its use.
5.When we use ref , data can be passed bi-directionally.When we use out data is passed only in a unidirectional way (from the called method to the caller method).
6.Both ref and out are behave differently at run time and they are treated the same at compile time.
7.Properties are not variables, therefore it cannot be passed as an out or ref parameter.

Example:-How to use ref and out keyword in c#?
using System;
class Program
    static void Main()
 int i = 0;
 Console.WriteLine(i); // Still i=0!
 TestMethod2(ref i);
 Console.WriteLine(i); // Now i=2!
 TestMethod3(out i);
 Console.WriteLine(i); // Now i=3!
    static void TestMethod1(int j)
 j= 1;
    static void TestMethod2(ref int j)
 j= 2;
    static void TestMethod3(out int j)
 j= 3;
Output:-Run your console application using F5 and output is:-

The out and ref keywords are use when we want to return a value in the same variables as are passed as an parameter.

You have just read an article that categorized by title C# by title What is Ref and Out in c# ?. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/06/part-22what-is-ref-and-out-in-c.html. Thank You!
Author: Bikesh Srivastava - Thursday, 30 June 2016

There are currently no comments for "What is Ref and Out in c# ?"

Post a Comment

Life Is Complicated, But Now programmer Can Keep It Simple.