Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / C# 4.0
Tip/Trick

ref and out Parameters in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
3 Jul 2014CPOL2 min read 33.5K   11   3
ref and out parameters in C#

Introduction

We can send parameters to functions using different methods for different type of objects, in C# values of basic data types such as int, float, char, etc. are sent as pass-by-value and values of user defined data types or object values are sent by as pass-by-reference. However, C# gives the flexibility to send values of your basic data type as pass-by-reference too, for that ref keyword is used. There is some situation when we did not pass a value to function but we want to get value for the function such as pass a number to the function and want to get its remainder and dividend from the function. To handle this situation, C# has a keyword out that is used to pass value from a function to the caller function.

ref Parameter in C#

ref parameter in C# is used to send basic data type by pass-by-reference. ref variable can’t be used for initialization purposes. ref variable is defined in function definition and function call. Ref variable must be assigned a value before the first call because the compiler will consider as valid reference and pass it to the calling function. If we did not assign it value, the parameter in the function will get a reference pointing to nothing, thus this is a programming error.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class refexam {
public void show(ref int x)
{
x = x / 2;
}
}
class Program
{
static void Main(string[] args)
{
int a;
refexam obj = new refexam();
/*by uncommenting the call compiler will generate an error and com will not compile*/
// obj.show(ref a);
a = 35;
Console.WriteLine("before call a :" + a);// this will display 35
obj.show(ref a);
Console.WriteLine("after call a :" + a);// this will display 17
Console.ReadKey();
}
}
}

ref variable is also used to send object to a function as reference-by-reference. In C#, object reference usually sends reference-by-value meaning the reference of a variable is copied to other variable. They are pointing to the same object but have their own copy of reference. The parameter variable cannot change the original variable reference.

Sending reference-by-reference is a method that both the real variable and the parameter variable as the same copy of reference, the parameter variable in the calling function can change the object pointed by the original variable or by variable in the caller function.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class refexam {
private int x;
private int y; 
public void set(int a,int b)
{ 
y = a;
x =b;
}
public void show()
{
Console.Write("value of y : ");
Console.WriteLine(y);
Console.Write("value of x : ");
Console.WriteLine(x);
}
public void change(ref refexam a,refexam b)
{
a = b;
}
}
class Program
{
static void Main(string[] args)
{
refexam obj = new refexam();
refexam obj1 = new refexam();
refexam obj2 = new refexam();
obj.set(5,6);
obj1.set(11,3);
/*before*/
Console.WriteLine(" before obj is");
obj.show();
Console.WriteLine(" before obj1 is");
obj1.show();
Console.WriteLine(" before obj2 is");
obj2.show();
obj1.change(ref obj1,obj2);
/*after call obj1 & obj2*/
Console.WriteLine(" after obj1 is");
obj1.show();
Console.WriteLine(" after obj2 is");
obj2.show();
Console.ReadKey();
}
}
}

out parameter in c#

out parameter can only be used to pass a parameter value out of the function. Out can be used without initialization but it must be assigned a value before returning from the function for which the out parameter is used. This parameter can be used for initialization purposes.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class refexam {
public void show(ref int x,out int y)
{
y = x % 2;
x = x / 2;
}
}
class Program
{
static void Main(string[] args)
{
int a;
int reminder;
refexam obj = new refexam();
/*by uncommenting the call compiler will generate an error and com will not compile*/
// obj.show(ref a);
a = 35;
Console.WriteLine("before call a :" + a);// this will display 35
//Console.WriteLine("before call reminder :" + reminder); this will not compile
obj.show(ref a,out reminder);
Console.WriteLine("after call a :" + a);// this will display 17
Console.WriteLine("before call a :" + reminder); // This will work because the calling function will initialize this
Console.ReadKey();
}
}
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionThanks Pin
Ivandro Ismael6-Jul-14 5:39
Ivandro Ismael6-Jul-14 5:39 
The `out` parameter is mostly used when you are returning more than 1 `type` and you don't want to use a object on return type
Questionhmmm.. Pin
johannesnestler3-Jul-14 2:32
johannesnestler3-Jul-14 2:32 
AnswerRe: hmmm.. Pin
pank.gupta3-Jul-14 5:53
pank.gupta3-Jul-14 5:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.