using System;
using ;
using ;
using ;
namespace RefAndOut
{
class Program
{
static void Main(string[] args)
{
int age = 10;
IncAge(age);
(The value of age in the Main function is: "+age);//Print out 10
int score = 80;
IncScore(ref score);
(The value of score in the Main function is: " + score);//Print out 81
int i=99;
Init(out i);
(The value of i in the Main function is: " + i);//Print out 10
();
}
public static void IncAge(int myAge)
{
myAge++;
(The value of myAge in the "IncAge function is:" + myAge);//Print out 11
}
public static void IncScore(ref int myScore)
{
myScore++;
(The value of Myscore in the IncScore function is:" + myScore);//Print out 81
}
public static void Init(out int ii )
{
ii = 10;
(The value of ii in the Init function is:" + ii);//Print out 10
}
/*
* Description: The method in C# passes the value regardless of the type of the parameter (value type or reference type), the default is "value pass". Except for ref and out.
* In the above code, after the IncAge method is called, the value of the parameter myAge parameter of the method has changed, but it will not affect the value of the age variable in the Main function.
* Even if I name the parameters of the IncAge function "age", the value of the aging variable in the Main function will not change. Because it is not the same variable at all (see: variable scope).
* When the IncScore function is called, the parameter myScore (ref type) is changed, which directly affects the value of the score variable in the external Main function.
* From this we can see that when using a ref parameter, the "reference" of the parameter is passed, which will affect the value of the variable defined outside the function.
* In the last Init, Out type output parameters are used. It also has an impact on the outside of the function. Out type parameter is suitable for assigning initial values to external variables in functions.
*/
}
}