Often using functions, the following is a summary of the important contents of the function-parameters,
The arguments passed by a function call in C # can be divided into 4 classes: A value parameter, a reference parameter, an output parameter, an array parameter. The following one by one explain to them
1. Value parameter
He is the type of argument that we often call, essentially a copy of the argument, and does not operate on the argument.
Class program { static void Main (string[] args) { Class1 class1 = new Class1 (); Class1.show ("Pass a Message"); } } Class Class1 {public void Show (String message) { Console.WriteLine (message); Console.ReadLine (); } }
2. Reference parameters (reference parameter)
The primary pass is a reference pointer to a parameter that assists in the execution of the delivery address. It means that it is worth the time to pass the address, which can operate on the argument, and the copy value of the formal parameter is different.
A function that uses a ref type parameter can be used to process parameters that are passed over externally.
static public void arr (ref int[] arr) { arr[0] = +; ARR[2] = 6634; ARR[4] = 0; } static void Main (string[] args) { int[] arr = new Int[8] {1, 2, 3, 4, 5, 6, 7, 8}; Arr (ref arr); for (int i = 0; i < arr. Length; i++) { Console.WriteLine (arr[i]); Console.ReadLine (); The result is a 100,2,6634,4,0,6,7,8, and the visible reference parameter operates on the actual parameter. } }
3. Output parameters (parameter)
The output parameter is also a reference pass, which is used when the function exceeds more than one return value. The Out keyword is typically used to have multiple return values for a method.
static public void arr (out int[] arr) { //initialize array arr = new Int[8] {111, 113, 117, 118};
} static void Main (string[] args) { int[] arr; Arr (out arr); for (int i = 0; i < arr. Length; i++) { Console.WriteLine (arr[i]); Console.readkey (); } Result 111,112,113,114,115,116,117,118 }
From the comparison above, both ref and out are passed the address of the parameter. The difference, however, is that the ref parameter of the array type must be explicitly assigned by the caller, and the out parameter of the array type must be assigned first by the defined function before it is used.
Ref is somewhat like a pointer in the C language. Be sure to assign a value before passing the parameter. Ref is not allowed to pass ref string
The Out parameter call does not have to be initialized, that is, no assignment. and must be assigned at least once within the function being called. However, properties cannot be passed as out parameters.
4. Array-parameters (array parameter)
The params type parameter type is primarily used for declaring functions without knowing the length of the array.
static public void Add (params int[] args) { int Count = 0; foreach (int A in args) { Count + = A; } Console.WriteLine ("0", Count); Console.readkey (); } static void Main (string[] args) { int[] arr = new Int[8] {1, 2, 3, 4, 5, 6, 7, 8}; ADD (arr); }
This type of parameter has a good effect on the connection of the data.
Parameters and return values