Search
Sponsor
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Forums Author
Sun, 23-Nov-2014 Authors
Pradeepk
10
All Time Authors
Jayakumars
4210
Karthikanbarasan
3455
Chvrsri
3140

Latest members | More ...


(Statistics delayed by 5 minutes)
 Forum Post

Difference between ref and out parameters in C# [Resolved]

Karthikanbarasan
Posted by Karthikanbarasan under C# on 1/11/2011 9:47:16 AM | Points: 10 | Views : 59180 | Status : [Member] [Moderator] [Microsoft_MVP] [MVP] | Replies : 6


Can any one explain the difference between ref and out parameters in details with examples

Thanks
Karthik
www.f5Debug.net


Posted by: Dn2010 on: 1/11/2011 [Member] Starter | Points: 25

hi Karthikanbarasan,

Ref and Out Parameters:

Both the parameters passed by reference, While for the Ref Parameter you need to initialize it before passing to the function and out parameter you do not need to initialize before passing to function.

you need to assign values into these parameter before returning to the function.

Ref (initialize the variable)
int getal = 0;
Fun_RefTest(ref getal);


Out (no need to initialize the variable)
int getal;
Fun_OutTest(out getal);


The out and the ref parameters are used to return values in the same variables, that you pass an an argument of a method. These both parameters are very useful when your method needs to return more than one values.

In this article, I will explain how do you use these parameters in your C# applications.

The out Parameter

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 class mathClass
{
public static int TestOut(out int iVal1, out int iVal2)
{
iVal1 = 10;
iVal2 = 20;
return 0;
}
public static void Main()
{
int i, j; // variable need not be initialized
Console.WriteLine(TestOut(out i, out j));
Console.WriteLine(i);
Console.WriteLine(j);
}
}

The ref parameter

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.

You can even use ref for more than one method parameters.

namespace TestRefP
{
using System;
public class myClass
{
public static void RefTest(ref int iVal1 )
{
iVal1 += 2;
}
public static void Main()
{
int i; // variable need to be initialized
i = 3;
RefTest(ref i );
Console.WriteLine(i);
}
}
}

Best Regards.

Karthikanbarasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suresh.mekkattil on: 1/11/2011 [Member] Bronze | Points: 25

HI,
Ref and out parameter passing modes are used to allow a method to alter variables passed in by the caller. The difference between ref and out is subtle but important. Each parameter passing mode is designed to apply to a slightly different programming scenario. The important difference between out and ref parameters is the definite assignment rules used by each.

The caller of a method which takes an out parameter is not required to assign to the variable passed as the out parameter prior to the call; however, the callee is required to assign to the out parameter before returning.


-----
Suresh M

Karthikanbarasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ck.kislay on: 1/12/2011 [Member] Starter | Points: 25

When we pass a parameter as ref to a method, the method refers to the same variable and changes made will affect the actual variable.Even the variable passed as out parameter is similar to ref, but there are few implementation differences when you use it in C# .

Argument passed as ref must be initialized before it is passed to the method, where as in case of out its is not necessary,but after a call to the method as an out parameter the variable must be initialized.


Thanks,
Chandan

Karthikanbarasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/12/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Resolved

Thanks all!!!

Thanks
Karthik
www.f5Debug.net

Karthikanbarasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tapan.Gupta@Yahoo.Co.In on: 9/3/2012 [Member] Starter | Points: 25

ref keyword
Passing variables by value is the default. However, we can force the value parameter to be passed by reference. Note: variable "must" be initialized before it is passed into a method.
out keyword
out keyword is used for passing a variable for output purpose. It has same concept as ref keyword, but passing a ref parameter needs variable to be initialized while out parameter is passed without initialized.
It is useful when we want to return more than one value from the method.
Note: You must assigned value to out parameter in method body, otherwise the method won't compiled.

TAPAN

Karthikanbarasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/6/2012 [Member] Bronze | Points: 25


Ref parameter Should be initilized before call.
Out need not to initilized before call.

class TestOut
{
static void FillArray(out int[] arr)
{
// Initialize the array:
arr = new int[5] { 1, 2, 3, 4, 5 };
}

static void Main()
{
int[] theArray; // Initialization is not required

// Pass the array to the callee using out:
FillArray(out theArray);

// Display the array elements:
System.Console.WriteLine("Array elements are:");
for (int i = 0; i < theArray.Length; i++)
{
System.Console.Write(theArray[i] + " ");
}

// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
/* Output:
Array elements are:
1 2 3 4 5
*/

class TestRef
{
static void FillArray(ref int[] arr)
{
// Create the array on demand:
if (arr == null)
{
arr = new int[10];
}
// Fill the array:
arr[0] = 1111;
arr[4] = 5555;
}

static void Main()
{
// Initialize the array:
int[] theArray = { 1, 2, 3, 4, 5 };

// Pass the array using ref:
FillArray(ref theArray);

// Display the updated array:
System.Console.WriteLine("Array elements are:");
for (int i = 0; i < theArray.Length; i++)
{
System.Console.Write(theArray[i] + " ");
}

// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
/* Output:
Array elements are:
1111 2 3 4 5555
*/

Karthikanbarasan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response


Found interesting? Add this to: