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.
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();
a = 35;
Console.WriteLine("before call a :" + a);obj.show(ref a);
Console.WriteLine("after call a :" + a);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.
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);
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);
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.
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();
a = 35;
Console.WriteLine("before call a :" + a);obj.show(ref a,out reminder);
Console.WriteLine("after call a :" + a);Console.WriteLine("before call a :" + reminder); Console.ReadKey();
}
}
}