These are defined for a method to make them dynamic. Parameters of a method can be of two types. Those are Input Parameters and Output parameters.
Input parameters are used for bringing a value into the method for execution where as output parameters are used for carrying a value out of the method after execution. By default every parameter is an input parameter and if we want to declare a parameter as output parameter we need to prefix the parameter either with ref/out keywords. A method can have any number of parameters.
We can give the results of a method after execution using a return type as well as output parameters also where a return type can give you only one single result but an output parameter can give multiple results. Output parameters are same as using pass by reference(implicit pointers) in out traditional C, C++ languages.
We will discuss about Input and Output parameters with simple console application. create MathClass and Add() method which takes two input parameters and one parameter as output parameter as shown below.
public class MathClass
{
public void Add(int a, int b, out int c)
{
c = a + b;
}
}
In the above code, a and b are input parameters and c is output parameter. We can call this Add() method by creating the object for MathClass as shown below.
class Program
{
static void Main(string[] args)
{
MathClass obj = new MathClass();
int sum;
obj.Add(1, 2,out sum);
Console.WriteLine(sum.ToString());
Console.ReadLine();
}
}
As shown above we are passing sum variable as out parameter and it produces the sum of two input parameters.
We can pass the default values to both input and output parameters like below.
public class MathClass
{
public void Add(out int c, int a = 100, int b = 200)
{
c = a + b;
}
}
class Program
{
static void Main(string[] args)
{
MathClass obj = new MathClass();
int sum;
obj.Add(out sum);
Console.WriteLine(sum.ToString());
Console.ReadLine();
}
}
As shown above we are providing the default values for two input parameters and while calling the method we are not passing any values to input parameters. If we are not passing values to input parameters then method will take default values and provides the result. In the above Add() method will take default values and provides the result as 300.
Remember one thing, while providing the default values to any parameter, you have to declare those parameters in the end, otherwise it will give compile time error as "optional parameters must appear after all required parameters". That means first we have to declare required parameters then we have to declare optional parameters.
You can use ref keywords also like out keyword as shown below, but in the case of ref parameters you must initialize the ref parameters.
public class MathClass
{
public void Add( int a, int b,ref int c)
{
c = a + b;
}
}
class Program
{
static void Main(string[] args)
{
MathClass obj = new MathClass();
int sum = 0;
obj.Add(100, 200, ref sum);
Console.WriteLine(sum.ToString());
Console.ReadLine();
}
}
As shown above we are using the ref keyword instead of out to declare the output parameters, but if you observe we are initialize the ref parameters while passing to the method(int sum = 0;).
Passing the default values to parameters of method is new feature added in C# 4.0. if a parameter is defined with default value then it becomes optional i.e, passing values to those parameters will become optional and if values are not passing it will internally takes default values.