One, out modifier
1. Calling a method with an output parameter also requires an out modifier, but local variables passed as output variables do not need to be assigned before they are passed as output variables (because they are changed or lost after the call), and the compiler allows you to pass the unassigned data because it is within the called method The department must contain data allocations for such variables.
2, a useful use, is to call a method to return multiple return values
static void Add (out int x, out int y, out int ans)
{
x = 1;
y = 2;
Ans = x + y;
}
3, note that the output parameter is defined, you must assign a valid value to the parameter before exiting the method, or it will cause a compiler error.
Second, ref modifier
This illustrates the main difference between out and ref;
1. If you want a method to operate on different data declared in the caller's scope (usually by changing its value), you must use a reference parameter.
2.ref reference parameters must be initialized before they are passed to the method , because they are passed a reference to an existing variable, and if they are not assigned a value, it is equivalent to manipulating an unassigned local variable.
Third, params modifier
The params keyword can pass a variable number of arguments (the same type) as a single logical parameter to the method. In other words, create a function calculateaverage (), which allows the caller to pass in any number of arguments and return the calculated average. If we define a params to receive the double[] data type, the caller only needs to pass in a comma-delimited double list: The NET runtime automatically wraps this set of double into an array of type double in the background.
static void Calculateaverage (params double[] valus) {}//method
Invocation type: Double Average=calculateaverage (4.0,3.3,4.2,5.6);//comma-separated argument list of type Double.
Double average=calculateaverage (data);//data is an array, double[] data={4.0,3.3,4.2,5.6}.
Calculateaverage ();//null value call.
A brief introduction to the out modifier, ref modifier, and params modifier for C #