Method Parameters In C#

In C# we can pass four types of parameter.

1) Value Parameters
2) Reference Parameters
3) Output Parameters
4) Parameters Array

-------------------------------------------------------------------------------
1) Value Parameters :-
a)When we pass value parameter then value of one variable copy to the other one.
b) And when we change the value of second variable it will no changes in the first variable value.
c) Value parameter create new storage area.
d) For example :-
int a;
-------------------------------------------------------------------------------

2) Reference Parameters :-
a) When we pass reference parameter then reference of one variable is assign to the other one.
b) And when we change the value of second variable it will make changes in the first variable value.
c) Reference parameters does not create new storage area.
d) We use the "ref" keyword while we pass the variable reference.
e) For example :-
ref int a;
-------------------------------------------------------------------------------

3) Output Parameters :-
a) Output parameter behave little bit different from the rest of parameters.
b) Instead of passing value to the called function, it is used to get back the result performed by the called function to the calling function.
c) We use the "out" keyword while we use the output parameters.
d) For example :-
out int a;
-------------------------------------------------------------------------------

4) Parameter Array :-
a) When we need to pass an array to the called function, then we use the parameter array.
b) It must be the last parameter in the called function and must be one dimensional .
c) We use the "params" keyword while use the parameter array.
d) Never use the "ref" and "out" keyword with the "params" keyword.
e) For example :-
out  params int[] arr        --------> it is wrong.
out int a, params int[] arr --------> it is right.

Method Parameters In C# Method Parameters In C# Reviewed by Baljeet Singh on 01:25 Rating: 5

No comments:

Powered by Blogger.