The following four types of parameters commonly used in C # are sorted out to facilitate system learning. The
One, pass parameters by value
Value parameter is implemented by value to the method, which is what we usually say by value, by copying the value of the argument to the formal parameter. When the
method is invoked, the CLR does the following:
1, allocating space for the parameter in the managed stack,
2, copying the value of the argument to the formal parameter.
This is too often used, and passing arguments by value is a copy, so it does not affect the value of the original parameter.
Class program { public static int foo (int i, out int  J) { i++;
j = 100;
return i + j;
   &NBSP} public static int foo (Int i,int j) { i++;
j++;
return i + j;    &NBSP} static void main (String[] args) { int i = 1;
int j = 2; int k =&nBsp
Foo (I, J); console.writeline (i);//1
console.writeline (j);//2 console.writeline (k);//5   &NBSP}}
The
two, passing arguments by reference-keyword ref
and the preceding "by value" corresponds to passing by reference. As the name suggests, the pass is not a value, but a reference. Note that this is not about passing a replica, but about uploading the true self to the method.
Note points:
1, Parameters passed by reference, the system no longer allocates new memory for formal parameters in the managed stack.
2, at this point, the formal parameter name is actually an alias for the argument name, and they point to the same memory location in pairs.
Class program { public static int foo (int i, out int  J) { i++;
j = 100;
return i + j;    &NBSP} public static int foo (Ref int i,ref  INT J) { i++;
j++;
return i + j;    &NBSP} static void main (String[] args) { int i = 1;
int j = 2; &Nbsp;int k = foo (REF I, REF  J); console.writeline (i);//2
console.writeline (j);//3 console.writeline (k);//5   &NBSP}}
Three, output parameters? keyword out
Output parameters and reference parameters are somewhat similar, and output parameters can be used to pass a value from within a method to a method, which is actually equivalent to having multiple return values. To use an output parameter, you only need to replace the REF keyword of the reference parameter with the Out keyword. However, it is important to note that only variables are eligible as output parameters, and literal values and expressions are not acceptable.
Note Two questions:
1, the compiler allows you to read the value of a reference parameter anywhere in the method, at any time.
2, the compiler prohibits reading it before assigning a value to an output parameter.
This means that the initial value of the output parameter is essentially meaningless because it is given a new value before it is used. Therefore, the way to pass the value to the method through the output parameter is not feasible.
class program { public static int foo ( INT I, OUT INT J) { i++;
j = 100;
return i + j;   &NBSP} static void main (string[] args) { int i = 1;
int j = 2;
int k = foo ( I, OUT  J); console.writeline (i);//1 Console.WriteLine (j);//100 console.writeline (k);//102  &NBSP}}
Four, parameter array? keyword params
parameter array:
Public class program { static void main (String[] args) { int count1 = plus (1); //Output 1
Console.WriteLine (COUNT1); int count2 = plus (1, 2, 3)//Output 6
Console.WriteLine (Count2); int count3 = plus (); //output 0 parameter array itself optional, no incoming value will not error { console.writeline (COUNT3); }
console.readkey ();        &NBSP} public static int plus (params int[] values) { int count = 0; foreach (int i in values ) {
count = count + i; } return count; } }
Additional two C # 4.0 new attributes optional and named parameters:
1, optional parameters
Optional parameters, as the name suggests, it is not required. For general parameters, if you do not specify a value for it, you may export a run error. However, optional parameters do not.
Rules for Optional parameters:
1, the optional parameter cannot be the first argument in the argument list, it must be after all the required parameters;
2, optional parameter must specify a default value;
3, the default value of an optional parameter must be a constant expression;
4, all optional parameters must be optional parameters.
Public class program { static void main (String[] args) { int count1 = plus (5); //is the default value when optional parameters are not specified Console.WriteLine (count1); //output 15 int count2 = plus (5,5); //When an optional parameter is specified, there is a default value console.writeline (count2); //output 10
console.readkey ();        &NBSP} public static int plus (INT I, INT J = 10) {
return i + j; } }
The
2, named parameter
Optional parameter resolves the problem of the default value of the parameter, and the named parameter solves the problem of the order of the arguments, which frees us from the list of parameters in which we remember each method in a large number. Allows you to enter parameters without order.
public class program
{
static void Main (string[] args)
{
//string str = "string";
int i = ten;
Console.WriteLine (Plus (STR:STR,I:I)); Although it's weird, these 3 lines of code are
Console.WriteLine (Plus (str: "string", i:10) that work. Note that the order is not the same as the method signature parameter
Console.readkey ();
Public
static string plus (int i, string str)
{return
str + i.tostring ();
}
}