There are mainly four types of method parameters.
- Value Type
- Reference Type
- Out parameters
- Parameter Arrays
Before discussing them, let's find out the difference between the two terms- Method Parameters and Method Arguments:
For e.g., we have a method given below.
- public static void test(string p)
- {
-
- }
In the above method method, p is called method parameter. On the other hand, if we try to invoke this method, the command will be, as given below.
In this case "abc" is the method argument.
Let's discuss different types of method parameters.
Value Type
In value type method parameters, argument and parameter holds different locations in the memory. Thus, if the value of the parameter gets changed, it will not affect the value of the argument.
The example is given below.
- class Program
- {
- static void Main(string[] args)
- {
-
- int a = 100;
- MethodForValueType(a);
- Console.WriteLine(a);
- }
- }
-
-
-
-
- public static void MethodForValueType( int b)
- {
- b= 20;
- }
- }
In the example shown above, we passed a=100 to "MethodForValueType", the copy of the same value will be created in the memory and b will hold that value. In this case, a and b will occupy two different places in the memory.
If we change the value of b, it will not affect the value of a because both variables are referring to different memory locations.
The value of a will remain 100, as shown above.
Reference TypeIn Reference type method parameters, argument and parameter have the same location in the memory. If the value of the parameter gets changed, it will not effect the value of the argument.
The example is given below.
-
-
-
-
- class Program
- {
- static void Main(string[] args)
- {
-
- int a = 100;
- MethodForRefType(ref a);
- Console.WriteLine(a);
- }
-
-
- public static void MethodForRefType(ref int b)
- {
- b = 20;
- }
- }
Now, as we pass a=100 to MethodForRefType, a and b will point to the same reference in the memory i.e. location of the variable a. If we change the value of b, it will effect the value of both the variables because they are referring to the same memory location.
Lets see the output for a in the screenshot given below.
Output parameters
If we want to return one value from a function, we use return type methods.But if we need to return more than one different values from a single method then we make use of output parameters.
Let's have an example.
- class Program
- {
- static void Main(string[] args)
- {
-
- int a = 100, b = 80;
- int Total = 0,subtraction = 0;
-
- SumSub(a, b, out Total, out subtraction);
- Console.WriteLine("Sum is: {0}", Total);
- Console.WriteLine("Sub is:{0}", subtraction);
-
- public static void SumSub(int a, int b, out int Sum, out int Sub)
- {
- Sum = a + b;
- Sub = a - b;
- }
- }
Parameter Array- As the name suggests, the parameter array is used, where we need to pass an array as a parameter.
- For the parameter array, we need to use keyword array.
- If we want to make an array as optional we can use the keyword params, else it will work same as value type, as shown below.
- class Program
- {
- static void Main(string[] args)
- {
- int[] numbers=new int[3];
- numbers[0]=1;
- numbers[1]=2;
- numbers[2]=3;
-
- PrintFromArrayParameter(numbers);
- PrintFromArrayParameter();
- }
-
-
-
- public static void PrintFromArrayParameter(params int[] numbers)
- {
- foreach (var item in numbers)
- {
- Console.WriteLine(item);
- }
- }
- }
Let's see the output that we get, as shown below.