Introduction
Usually methods take parameters. There are many ways to pass the parameters and for this C# provides some parameter modifiers. Look at them below.
- None (or default) parameter: If a parameter is not attached with any modifier, then the parameter's value is passed to the method. This is also known as call-by-value and it is the default for any parameter.
- Ref (reference) parameter: If a parameter is attached with a ref modifier, then changes will be made in a method that affect the calling method. This is also known as call-by-reference.
- Out (output) parameter: If a parameter is attached with an out modifier, then we can return a value to a calling method without using a return statement.
- Params (parameters) parameter: If a parameter is attached with a params modifier, then we can send multiple arguments as a single parameter. Any method can have only one params modifier and it should be the last parameter for the method.
Let's see all one by one by using them in a program.
C# Default Parameter
By default, the parameters are passed to a method by value. So, the changes made for parameters within a method will not affect the actual parameters of the calling method. Look at the following program which tries to swap the number but does not succeed just becausecall-by-value does not affect the actual parameter.
using System;
class Program
{
public static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
static void Main(string[] args)
{
int x = 10, y = 12;
Console.WriteLine("Before: x={0}, y={1}", x, y);
Swap(ref x, ref y);
Console.WriteLine("After: x={0}, y={1}", x, y);
Console.ReadKey();
}
}
Output
C# ref Parameter
Now, let's swap by using a ref (reference or call-by-reference) modifier.
- Whenever we want to allow changes to be made in a method, then we will go for call by ref. The following are the differences between output (out) and reference (ref) parameters.
- The output (out) parameters do not need to be initialized before use in a called method. Because it is assumed that the called method will providethe value for such a parameter.
The reference (ref) parameters must be initialized before sending to called method. Because we are passing a reference to an existing type and if we don't assign an initial value, it would be equivalent to working on a NULL pointer.
Let's have a program which swaps a value.
using System;
class Program
{
public static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
static void Main()
{
int x = 10, y = 12;
Console.WriteLine("Before: x={0}, y={1}", x, y);
Swap(ref x, ref y);
Console.WriteLine("After: x={0}, y={1}", x, y);
Console.ReadKey();
}
}
Output
Look at one another program.
using System;
class Program
{
public static void MyFun(ref string s)
{
s = s.ToUpper();
}
static void Main()
{
string s = "abhimanyu";
Console.WriteLine("Before: {0}", s);
MyFun(ref s);
Console.WriteLine("After: {0}", s);
Console.ReadKey();
}
}
Output
C# out Parameter
In some of the methods, we need to return a value to a calling method. Instead of using a return statement, for this C# provides a modifier for a parameter as out. The usage of out can be better understood by the following program.
using System;
class Program
{
public static void Add(int x, int y, out int z)
{
z = x + y;
}
static void Main()
{
int x = 10, y = 12, z;
Add(x, y, out z);
Console.WriteLine("z = {0}", z);
Console.ReadKey();
}
}
Output
The out parameter is certainly useful when we need more values to be returned from a method. Consider one more program.
using System;
class Program
{
public static void CallFun(out int x, out string s)
{
x = 10;
s = "My name is Abhimanyu.";
}
static void Main()
{
int x;
string str;
CallFun(out x, out str);
Console.WriteLine("x = {0}, str = {1}", x, str);
Console.ReadKey();
}
}
Output
C# params Parameter
The params keyword of C# allows us to send multiple arguments as a single parameter. The usage of params can be better understood by the following program.
using System;
class Program
{
public static void CallFun(params int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i]);
Console.Write(", ");
}
}
static void Main()
{
int[] x = new int[4] { 10, 12, 15, 17 };
int a = 20, b = 1988;
CallFun(x);
CallFun(a, b);
Console.ReadKey();
}
}
Output
In the above program we can observe that for the params parameter, we can pass an array or set of individual elements. We can use params even when the parameters to be passed are of different types; look at the program.
using System;
class Program
{
public static void CallFun(params object[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] is int)
{
Console.WriteLine("{0} is an integer.", arr[i]);
}
if (arr[i] is string)
{
Console.WriteLine("{0} is a string.", arr[i]);
}
if (arr[i] is bool)
{
Console.WriteLine("{0} is a boolean.", arr[i]);
}
}
}
static void Main()
{
int x = 10;
string s = "abhimanyu";
bool b = true;
CallFun(b, x, s);
Console.ReadKey();
}
}
Output
So, that's all about the Method Parameter Modifiers in C#.
Thanks for reading.