The out and the ref parameters are used to return values in the same variables, that you pass 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);
}
}
}
m kamran khalid chPosted Mar 10, 2013, 10:42 AM
Aallaaaaa.............!
prathapPosted Jan 1, 2013, 1:56 AM
The ref and out keywords are useful for the value type. Since value types are passed by value. If you want pass the value type as structure, you have to use ref or out modifier.
Mahesh ChandPosted May 30, 2012, 10:42 AM
Thanks!
Balaji RPosted May 11, 2012, 8:49 AM
good one
Yogesh UpretiPosted Nov 17, 2011, 12:44 AM
Thansk for providing such a nice info about the ref and out paramaeter.
Niraj DavePosted Aug 8, 2011, 9:40 AM
C++ orginal method is as below: apiStatus WINAPI rfidapi03_open (OUT HANDLE &hCom, char *com_port); How to declare variable and call above method from c# we have [Dllimport("seRFIDAPIce03.dll")] static extern unsafe apiStatus rfidapi03_open(out IntPtr hCom, ref byte com_port); but it is not working
veera veditedPosted Jan 6, 2011, 4:14 AMEdited Jan 6, 2011, 4:28 AM
Simple and Sweet., add this point also Out parameter should get assign before it is going use, means inside the method u must assign value for out parameter, no need assign value for ref parameter inside the method.
najla mohammedPosted Aug 24, 2010, 1:13 PM
Nice article, thank you so much.
anandeditedPosted Jan 17, 2010, 9:54 AMEdited Jan 17, 2010, 9:55 AM
Hello, its really very simple to understand and suitable example for out/ref. Thanks to c-sharpcorner n u too. Anand.
Bayu HerdiawanPosted Jul 22, 2009, 8:41 PM
Hello, what about i must to return many values from the class using the 'out' parameter ? can i use looping on it? thank you.
swatiPosted Feb 22, 2009, 12:00 AM
Hi, I am doing a project on license plate recognition system and i was trying my hands on thinning algorithm used to get the skeleton of the character to recognise it. I hav used ur C # basic helps to convert some c codes to c#. But my thinning algorithm just does not work . Plz help me if u can i will be highly obliged . The link to code in C is http://pages.cpsc.ucalgary.ca/~parker/thin.c
Sunil PawarPosted Jun 14, 2007, 7:03 AM
Hi, This is really good article. Can you please tell me which parameter is better to use and what is the difference between them? Because it seems that both of them is having same funtionality. Sunil Pawar
ajit singhPosted Jun 6, 2007, 3:40 AM
what are the advantages of abstract classes and static members in C#