Parameter modifiers in C# are entities that controls the behavior of the arguments
Passed in a method.
There are 4 types of Parameter modifiers in c#.
1) None - if there is NO parameter modifier with an argument, it is passed by value, where the method receives a copy of the original data.
e.g.
Public class TestClass
{
Public static int addTwo(int a, int b)
{
return a+b;
}
{
return a+b;
}
}
Wrong Way:
Public class TestClass
{
Public static int addTwo(int a, int b)
{
return a+b;
}
{
return a+b;
}
}
Inside Calling Method
int l,m,n; n= TestClass. addTwo (l, m);
or
int l,m=8,n; n= TestClass. addTwo (l, m);
or
int l=7,m,n; n= TestClass. addTwo (l, m);
Here we do not initialized input parametre l or m,which we are passing as argument to function so compiler raises an error.
Right Way:
Public class TestClass
{
Public static int addTwo (int a, int b)
{
return a+b;
}
{
return a+b;
}
}
int l=6,m=7,y; y= TestClass.addtwo (l, m);
Here we initialized input parametre l,m which we are passing as argument to function so no worry
2) out - argument is passed by reference. The argument marked with "out" modifier needs to be assigned a value within this function, otherwise a compiler error is returned.
e.g.
Public class TestClass
{
Public void MultiplyTwo (int a, int b, out int prod)
{
prod = a * b;
}
{
prod = a * b;
}
}
Wrong Way:
Public class TestClass
{
Public void MultiplyTwo (int a, int b, out int prod)
{
return prod;
}
{
return prod;
}
}
In Calling Function:
int a=6,b=7,prd=89; TestClass. MultiplyTwo (a, b, out prd)
Here we do not assigned any value to out parametre prod inside MultiplyTwo() function so compiler raises error even though you pass it initialized through calling method
Right Way:
Public class TestClass
{
Public void MultiplyTwo (int a, int b, out int prod)
{
prod = a * b;
}
{
prod = a * b;
}
}
In Calling Method:
int l=7,m=6,y; TestClass. MultiplyTwo (l, m,out y);
or
int l=7,m=6,y=78; TestClass. MultiplyTwo (l, m,out y);
Here as we have only one out parameter which we set in our function so there is no error in caller method parameter passed as out may or may not be initialized it works.
3) params- This modifier gives the permission to set a variable number of identical datatype arguments.
Note: 1) A method may have only one "params" modifier.
2) The params modifier needs to be in the last argument.
static int totalruns(params int[] runs)
{
int score = 0;
for (int x = 0; x < runs.Length;x++)
{
score += runs[x];
}
return score;
}
Further, from the calling function, we may pass the scores of each batsman as below...
score = totalruns(12,36,0,5,83,25,26);
4) ref - The argument is given a value by the caller, where data is passed by reference. This value may optionally be reset in the called method.
e.g.
Public class TestClass
{
Public static void substractTwo(int a, int b,ref res)
{
res=a-b
{
res=a-b
}
}
Wrong Way:
Public class TestClass
{
Public static void substractTwo(int a, int b,ref res)
{
res=a-b
{
res=a-b
}
}
In calling method
Int a=8,b=0,c;
TestClass. substractTwo(a,b,ref c)
Here as c is not initialized in calling method, which we are passing as ref parameter to substractTwo () function. Hence compiler raises error.
Right Way:
Public class TestClass
{
Public static void substractTwo (int a, int b,ref res)
{
res=a-b
{
res=a-b
}
}
Or
Public class TestClass
{
Public static void substractTwo (int a, int b,ref res)
{
/*do not modify res variable do anything else you want*/
{
/*do not modify res variable do anything else you want*/
}
}
In calling method:
Int a=8,b=0,c=89;
TestClass. substractTwo(a,b,ref c)
Here as c is initialized in calling method, which we are passing as ref parameter to substractTwo () function. Hence no compiler error. Further in second ‘substractTwo’ method we are not modifying parameter passed as reference yet this is not a restriction unlike in case of out.
Difference between ref & out parameter:
Output parameters and ref parameters are very similar in way we invoke method corresponding to them. The differences are as follows
- In Output parameters unlike reference parameters, the variable specified on the invocation doesn't need to have been assigned a value before it is passed to the function member. If the function member completes normally, the variable is considered to be assigned afterwards (so you can then "read" it).
- In Output parameters unlike reference parameters, the parameter is considered initially unassigned (in other words, you must assign it a value before you can "read" it in the function member).
- unlike reference parameters, the Output parameter must be assigned a value before the function member completes normally.
No comments:
Post a Comment