- Value
- Out
- Ref
- Params
1.Value parameters
This is the default parameter type in C#. If the parameter does not have any modifier it is "value" parameter by default. When we use "value" parameters the actual value is passed to the function, which means changes made to the parameter is local to the function and is not passed back to the calling part.
using System;
class ParameterTest
{
static void Mymethod(int Param1)
{
Param1=100;
}
static void Main()
{
int Myvalue=5;
MyMethod(Myvalue);
Console.WriteLine(Myvalue);
}
}
Output
5
Even though the value of the parameter Param1 is changed with in MyMethod it is not passed back to the calling part since value parameters are input only.
2. Out parameters
"out" parameters are output only parameters meaning they can only pass back a value from a function. We create a "out" parameter by preceding the parameter data type with the out modifier. When ever a "out" parameter is passed only an unassigned reference is passed to the function.
using System;
class ParameterTest
{
static void Mymethod(out int Param1)
{
Param1=100;
}
static void Main()
{
int Myvalue=5;
MyMethod(Myvalue);
Console.WriteLine(out Myvalue);
}
}
Output100
Since, the value of the "out" parameter is passed back to the calling part. The modifier "out" should precede the parameter being passed even in the calling part. "out" parameters cannot be used within the function before assigning a value to it. A value should be assigned to the "out" parameter before the method returns.
3.Ref parameters
"ref" parameters are input/output parameters meaning they can be used for passing a value to a function as well as to get back a value from a function. We create a "ref" parameter by preceding the parameter data type with the ref modifier. When ever a "ref" parameter is passed a reference is passed to the function.
using System;
class ParameterTest
{
static void Mymethod(ref int Param1)
{
Param1=Param1 + 100;
}
static void Main()
{
int Myvalue=5;
MyMethod(Myvalue);
Console.WriteLine(ref Myvalue);
}
}
Output105
Since, the "ref" parameter acts as both input and output. The modifier "ref" should precede the parameter being passed even in the calling part. "ref" parameters should be assigned a value before using it to call a method.
4.Params parameters
"params" parameters is a very useful feature in C#. "params" parameter are used when a variable number of arguments need to be passed. The "params" should be a single dimensional or a jagged array.
using System;
class ParameterTest
{
static int Sum(params int[] Param1)
{
int val=0;
foreach(int P in Param1)
{
val=val+P;
}
return val;
}
static void Main()
{
Console.WriteLine(Sum(1,2,3));
Console.WriteLine(Sum(1,2,3,4,5));
}
}
Output
6
15
The value passed for a "params" parameter can be either comma separated value list or a single dimensional array. "params" parameters are input only.
What is reference parameter? What is out parameters? What is difference these two?
- a 'ref' parameter must first be initialized before being passed from the calling function to the called function but
- a 'out' parameter need not be initialized, we can pass it directly.
- a 'ref' parameter can be used as both input and o/p parameter.
- a 'out' parameter can be used as only output parameter.