SIGN UP MEMBER LOGIN:    
ARTICLE

Facts of Ref And Out Type Parameters in C#

Posted by Abhimanyu Kumar Vatsa Articles | C# Language June 30, 2011
In this quick article you will take a look at the facts of ref and out type parameters in C#.
Reader Level:

Introduction

Parameters are initialized with a copy of the arguments; when we pass an argument to a method, this is true regardless of whether the parameter is a value type like int, int? (nullable) or a reference type. Look at the example given below, it is impossible to change the parameter to affect the value of the argument passed.

        static void Main(string[] args)
       
{
           
int num = 33;
           
myfunction(num);
           
Console.WriteLine(num);
           
Console.ReadKey();
       
}

        private static void myfunction(int rcnum)
       
{
           
rcnum++;
       
}

In the above example, the value to the console is 33 not 34. The myfunction method increments a copy of the argument named rcnum and not the original argument. C# Language provides the ref and out keywords to allow modification.

Ref Type Parameters

When we pass a parameter as a ref type to a method, the method refers to the same variable and changes are made that will affect the actual variable. If we add the ref prefix then the parameter becomes an alias for the actual argument rather than a copy of the argument. When we use the ref parameter, anything we do to the parameter also affects the original argument because the parameter and the argument both reference the same object. Look at the example given below:

        static void Main(string[] args)
       
{
           
int num = 33;
           
myfunction(ref num);
           
Console.WriteLine(num);
           
Console.ReadKey();
       
}

        private static void myfunction(ref int rcnum)
       
{
           
rcnum++;
       
}

In the above example, we have passed to the myfunction method a reference to the original argument rather than a copy of the original argument and because of this any changes the method makes also changes the original argument and results in 34 being displayed on the screen.

Out Type Parameters

A variable passed as an out parameter is similar to ref, but there are a few implementation differences when you use it in C#. An argument passed as ref must be initialized before it is passed to the method, whereas in case of out it is not necessary, but after a call to the method as an out parameter the variable must be initialized. Look at the example below which will clarify all your doubts:

        static void Main(string[] args)
       
{
           
int num;
           
myfunction(out num);
           
Console.WriteLine(num);
           
Console.ReadKey();
       
}

       
private static void myfunction(out int rcnum)
       
{
           
rcnum = 33;
       
}

In above example, we have not passed any value from the main method; this is the importance of the out type parameter.

Thanks for joining here.

HAVE A HAPPY CODING!!

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

Nice Article

Posted by Ganesan R Jul 06, 2011

Nice Article

Posted by Gobi G Jul 01, 2011

Thank you, Abhimanyu, this will help beginners. I think however that it would help to also say that reference types do not need to be created (with "new") before use as an out parameter but they must be created in the function. Also, perhaps "after a call to the method as an out parameter the variable must be initialized" might be unclear since the initialization is done in the call (in the function) instead of after the call.

Posted by Sam Hobbs Jun 30, 2011

thanks

Posted by Abhimanyu Kumar Vatsa Jun 30, 2011

Nice Article

Posted by Shalini Juneja Jun 30, 2011
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.
Nevron Gauge for SharePoint
Become a Sponsor