Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - What is the Difference between Ref and Out in c# with Example

Jun 17, 2014
Introduction:

Here I will explain difference between ref and out parameters in c#.net with example. Both ref and out parameters are used to pass arguments within a method. These ref and out parameters are useful whenever your method wants to return more than one value. Regarding ref parameter you need to initialize it before passing to the method and out parameter you don’t need to initialize before passing to function.    

Description:


Ref Parameter

If you want to pass a variable as ref parameter you need to initialize it before you pass it as ref parameter to method. Ref keyword will pass parameter as a reference this means when the value of parameter is changed in called method it get reflected in calling method also.

Declaration of Ref Parameter

Generally we will use ref parameters like as shown below


int i=3; // variable need to be initialized
Refsample(ref i);
If you observe above code first we declared variable and initialized with value 3 before it pass a ref parameter to Refsample method

Example


class Program
{
static void Main()
{
int i; // variable need to be initialized
i = 3;
Refsample(ref i);
Console.WriteLine(i);
}
public static void Refsample(ref int val1)
{
val1 += 10;
}
}
When we run above code we will get like as shown below

Output

*********Output************

13

*********Output************
As we discussed if ref parameter value changed in called method that parameter value reflected in calling method also

Out Parameter

If you want to pass a variable as out parameter you don’t need to initialize it before you pass it as out parameter to method. Out keyword also will pass parameter as a reference but here out parameter must be initialized in called method before it return value to calling method.

Declaration of Out Parameter

Generally we will use out parameters like as shown below


int i,j; // No need to initialize variable
Outsample(out i, out j);
If you observe above code first we declared variable and we it pass a out parameter to Outsample method without initialize the values to variables

Example


class Program
{
static void Main()
{
int i,j; // No need to initialize variable
Outsample(out i, out j);
Console.WriteLine(i);
Console.WriteLine(j);
}
public static int Outsample(out int val1, out int val2)
{
val1 = 5;
val2 = 10;
return 0;
}
}
If we observe code we implemented as per our discussion like out parameter values must be initialized in called method before it return values to calling method

When we run above code we will get like as shown below

Output

*********Output************
5
10

*********Output************

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

24 comments :

Bhagawat007 said...

Nice

Unknown said...

Very Nice Suresh

Hamid said...

Thanks Suresh, smart-1 Article

Pakya said...

Realy too good and simply explained

Anonymous said...

Easy way to understand ref and out parameter

Anonymous said...

very useful.......

Unknown said...

Nice article. Really helping.

Unknown said...

thanks sir , nice article...............

Anonymous said...

article is so helpful.....

Manjunath said...

Nicely dealt Sir,Thank You.

sunny yadav said...

Very good description of ref and out keyword purpose.

Prakash said...

nice explanation...

Hazrat said...

Very easy to understand. Nice article suresh

Anonymous said...

Nice simple explain...

Unknown said...

Excellent explation..

Unknown said...

Nice...

Unknown said...

good one (y)

Anonymous said...

Very good article but you said that ref and out parameters useful when the method has to return more than 1 value. How to achieve that.?

G Krishna Rao said...

Very well explained.

Unknown said...
This comment has been removed by the author.
Unknown said...

Very Nice

Anonymous said...

Thanks for sharing the knowledge , Very helpful !!

Common man said...

Simple and easy understanding, Example added well ..

Unknown said...

really great

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.