There are 4 different way we can pass the parameter in the C# methods which all are as below:
1. Value parameter
2. Ref parameter
3. Out parameter
4. Param parameter
1. Value parameter:
Pass the value of the variable to the method or function, this mean passing the local value of one function to another function and both the function variable value accessibility to the local only. If any value want to get from the function then we need to use return keyword.
Example:
Output:
2. Ref parameter:
Reference type parameter passing the address of the variable instead of value,from and to function name need to use
Note:
Example:
1. Value parameter
2. Ref parameter
3. Out parameter
4. Param parameter
1. Value parameter:
Pass the value of the variable to the method or function, this mean passing the local value of one function to another function and both the function variable value accessibility to the local only. If any value want to get from the function then we need to use return keyword.
Example:
static void Main(string[] args)
{
int num1 = 2;
int num2 = 3;
int result = 0;
//passing num1 and num2 value to function Mul
//return the multiplication value of num1 and num2
result = Mul(num1, num2);
Console.WriteLine("Two no. multiplication value is :" + result);
Console.ReadLine();
}
//Value Parameters Method
static int Mul(int num1, int num2)
{
return num1 * num2;
}
Output:
Two no. multiplication value is :6
2. Ref parameter:
Reference type parameter passing the address of the variable instead of value,from and to function name need to use
ref
keyword before the variable name.This will be helpfully when need to return multiple value from the function.
Note:
ref
is read and write that means from function variable should have some value then only you will able to pass the value otherwise compiler error will be thrown as "Error CS0165 Use of unassigned local variable 'variable name'".
Example:
static void Main(string[] args)
{
int num1 = 2;
int num2 = 3;
int result=0 ;
//passing num1 & num2 as value type
//Pass result as ref type to function Mul
Mul(num1, num2, ref result);
Console.WriteLine("Two no. multiplication value is :" + result);
Console.ReadLine();
}
//Reference Parameters Method
static void Mul(int num1, int num2,ref int result)
{
result=num1 * num2;
}
Output:
Two no. multiplication value is :6
In above example you can see there is no return value still we got the multiplication value in the result variable as "result" variable address's value is changed.
3. Out parameter:
Our parameter also passing the address of the variable instead of value same as
ref
,from and to function name need to use out
keyword before the variable name.This will be helpfully when need to return multiple value from function.
Note:
out
is optionally read and but must write that means from function variable can or can not have value,if you don't have value which is going to pass as out then no compiler error, this is the difference between ref and out.
Example:
static void Main(string[] args)
{
int num1 = 2;
int num2 = 3;
int result;
//passing num1 & num2 as value type
//Pass result as out type to function Mul
Mul(num1, num2, out result);
Console.WriteLine("Two no. multiplication value is :" + result);
Console.ReadLine();
}
//out Parameters Method
static void Mul(int num1, int num2,out int result)
{
result=num1 * num2;
}
Output:
Two no. multiplication value is :6
I. In above example you can see there is no return value still we got the multiplication value in result variable as "result" variable address's value is changed.
II. Also notice the
int result;
does not have value, in case of ref having value.
4. Params parameter:
Params parameter is usefully when we don't know no. of parameter need to pass in a function (but there are some constraint). By this we can pass no. of parameters to a variable by using
params
keyword and to function hold all the values in an array of same type.
Note:
I. No. of parameters should be same type.
II.
params
should be the last parameter of the function.
Example:
static void Main(string[] args)
{
//passing age as int
//passing emp name, dept and designation as params
ParamsExample(42, "Johan", "Software", "Software Engineer");
Console.ReadLine();
}
static void ParamsExample(int age,params string[] emp)
{
//print all params parameters
foreach (string empdata in emp)
{
Console.WriteLine(empdata);
}
}
Output:
Johan
Software
Software Engineer
Software
Software Engineer
No comments:
Post a Comment