0
what is output of the code?and explain here how argument pass,why we use test in these program
static int test (out int x,int y=4){ x=6 return x* y } static void Main () int a int z= Test(out a) console. WriteLine (a+z) }
1 Réponse
+ 3
Output: 30
This program is demonstrating the usage of "out" parameter and default argument. That's the reason they have used "test ()" method.
Usage of default argument is, if you didn't pass the value for that variable then default value in method signature will be assigned. In your case they didn't pass the value for Y. So compiler will assign 4 to "y" in test method.
test(a, 10); now 10 will assign to "y" instead of 4.
Assume the scenario, that you have to do some operation and return the resultant value and error status (no error, or some error codes). So here we are having two return values. As per method syntax you can return only one value. How to return the 2nd value?
There comes the "Out" parameter. You can return the error status and store the result in Out variable.
bool operations ( int nVal, out int val)
{
val = 0;
if (0==nVal) return false;
val = nVal * 2;
return true;
}
In the above example you can see, if nVal is zero then we are return false which indicates error occurred in this method. Otherwise we returning true (no error), and assigning product value.
Hope you understand this well.