SIGN UP MEMBER LOGIN:    
ARTICLE

The out and ref Paramerter in C#

Posted by Mahesh Chand Articles | C# Language June 12, 2001
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.
Reader Level:

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);
}
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

good one

Posted by Balaji R May 11, 2012

Thansk for providing such a nice info about the ref and out paramaeter.

Posted by Yogesh Upreti Nov 17, 2011

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

Posted by Niraj Dave Aug 08, 2011

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.

Posted by veera v Jan 06, 2011

Nice article, thank you so much.

Posted by najla mohammed Aug 24, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor