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.
- test("abc")
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.
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)
- {
- //Lets invoke MethodForValueType Method
- int a = 100;
- MethodForValueType(a);
- Console.WriteLine(a);
- }
- }
- //Let we have a method expecting int parameter.
- 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.
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 Type
In 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.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.
In 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)
- {
- //Lets invoke MethodForRefTypeMethod
- int a = 100;
- MethodForRefType(ref a);
- Console.WriteLine(a);
- }
- //Reference Type:
- //Let we have a method expecting reference int parameter.
- public static void MethodForRefType(ref int b)
- {
- b = 20;
- }
- }
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.
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)
- {
- //Lets invoke MethodForValueType Method
- int a = 100, b = 80;
- int Total = 0,subtraction = 0;
- //we can directly get the output from method in out parameters.
- 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;
- //because if define params keyword then it makes it optional.
- PrintFromArrayParameter(numbers);
- PrintFromArrayParameter();
- }
- public static void PrintFromArrayParameter(params int[] numbers)
- {
- foreach (var item in numbers)
- {
- Console.WriteLine(item);
- }
- }
- }


Manav PandyaPosted Jan 17, 2017, 5:13 AM
Do you have any example which shows property mapping with database in asp.net mvc ???
Manav PandyaPosted Jan 17, 2017, 5:12 AM
Nice article share Neha .......
gagandeep singhPosted Dec 28, 2016, 7:16 AM
Thanks for the article . i was looking for such an example.
Robert MinardiPosted Dec 27, 2016, 9:02 AM
The screen shot you have for Output Parameters is the same as for Reference Type. Otherwise, good submission.