In method parameter passing, you can use both the ref and out keywords, but be aware that the ref and out parameters are passed differently.
Using System;
Class program
{
static void Main ()
{
program obj = new program ();
int score = 55; Declare original score
int refscore, outscore;//Declare two variables
obj. Changescore (score, ref refscore, out outscore);
Console.WriteLine ("Your original score is {0}, the adjusted score is {1}, plus the normal performance after {2}",
Score, Refscore, outscore);
Console.readkey ();
}
private void Changescore (int score, ref int refscore, out int outscore)
{
if (Score > && Score <
{
refscore =;
if (Refscore >)
{
outscore = Refscore + 5;
}
}
}
The above code compiles with two errors, we only declare the variable refscore, and do not give the variable display assignment. So the first error occurred: The unassigned local variable "Refscore" was used. When the Out keyword passes a parameter, the parameter must be modified within the calling method, and the IF statement is used to determine that only the qualified out parameter is assigned, and the out parameter that does not meet the criteria is not assigned, so there is a "outscore assignment to the out parameter before controlling the Exit method" error.
Using System;
Class Program
{
static void Main ()
{
Program obj = new program ();
int score = 55; Declare original Score
int refscore = 0, outscore; Declaration of two variables
Obj. Changescore (score, ref refscore, out outscore);
Console.WriteLine ("Your original score is {0}, the adjusted score is {1}, plus the normal performance after {2}",
Score, Refscore, outscore);
Console.readkey ();
}
private void Changescore (int score, ref int refscore, out int outscore)
{
if (Score > && score < 60)
{
Refscore = 60;
}
if (Refscore > 60)
{
outscore = Refscore + 5;
}
Else
{
outscore = Refscore;
}
}
}
The following information is available for reference:
The difference between a value parameter and a reference parameter and an output parameter:
1. The value of the argument in the value parameter is not changed with the change of parameter value;
Parameter values are not affected, and when arguments are passed to formal parameters, they are allocated another part of the space in the stack. In this way, two parameter changes are not affected.
2. The values of parameters in reference and output parameters will change with the change of parameter value;
(Formal parameters: The parameters specified in the definition function, which do not account for memory cells when function calls are not present.) A parameter in a function is assigned an inner deposit element only when a function call occurs. At the end of the call, the memory unit that the formal parameter occupies is also freed. The value of the argument is assigned to the parameter when called);
In the reference and output parameters, the argument allocates space in the stack. When an argument is passed to a parameter, the address is passed to the formal parameter, which is the passed pointer, and the argument changes as the parameter is changed.
Two, the reference parameter and the output parameter difference:
1. When using ref and out parameters, both the parameters in the method and the calling parameters need to be added with a ref or out keyword.
2, the use of the ref parameter and the result of the out parameter is the same, the difference is: Ref before the call needs to give the parameters in the calling method to assign an initial value. Out does not need to assign an initial value to the parameters in the calling method before the call, and the parameters are initialized in the actual method body.
C # method parameter passing-using both the ref and out keywords