Basic Knowledge
What is a formal parameter, what is an argument, see the Knowledge:
ignored variable number parameters
Variable number parameter: a one-dimensional array that is decorated with the params keyword. The following is a simple example demo:
1 Static voidMain (string[] args)2 {3 4Shownumbersum ("parameter Pinning",1,2,3);5Shownumbersum ("variable Parameters",1,2,3,4,5);6 Console.read ();7 }8 9 Static voidShownumbersum (stringTempintIintJintk)Ten { OneConsole.WriteLine ("{0} and yes: {1}", temp, i + j +k); A } - - Static voidShownumbersum (stringTempparams int[] numbers) the { - intsum =0; - foreach(intIinchnumbers) - { +Sum + =i; - } +Console.WriteLine ("{0} and yes: {1}", temp, sum); A}View Code
params Keyword Usage Summary:
- The params-modified keyword must be a one-dimensional array
- The params can modify any type of parameter array, and if you need to accept any type parameter, the parameter can be written like this: params object[] O
- The params decorated parameter must be in the last digit of the parameter list, and only once in a method
The Art of parameter passing
Based on the similarities and differences between value types and reference types, parameter passing is summarized in the following four scenarios:
Value type parameters are passed by value
The value type parameter is passed by value in fact a copy of the value type instance that is called by the method operation belonging to its own instance copy, so it does not affect the parameter values in the original calling method
Reference type parameters are passed by value
The reference type parameter pass actually passes the copy of the object address, which is the same as the value type by value, passing the value, but the value that the reference type passes is the reference address.
Therefore, the value that is referenced after the method is called changes, thus changing the orientation of the original object, so the value of the original object has changed. The following is a simple instance code:
classProgram {Static voidMain (string[] args) {testcal TC=Newtestcal (); Changvalue (TC); Console.WriteLine ("Mark:"+tc.number1); Console.read (); } Static voidChangvalue (testcal tc) {tc.number1="New Value"; Console.WriteLine (Tc.number1); }}
class testcal { publicstring number1; Public testcal () { "testcal"; } }
The particularity of string types passed by value
Analyze the code: does the value of temp change after the method call?
classprogram{Static voidMain (string[] args) { stringtemp ="value"; ChangeValue (temp); Console.read (); } Static voidChangeValue (stringstr) {STR+=" Change"; Console.WriteLine (str); }}
Executes ChangeValue (temp); The reference address of the passed argument
Process str + = "+change"; Here is actually generated a new address to STR, the new address points to the value of "Value+change", the whole process did not change the original object's reference address
value type parameters and reference type parameters are passed by reference
By reference, the main thing is to use the ref and out keywords. The effect is the same for the twin brothers, but there are some differences.
Ref: parameter must be initialized
Out: The parameter can be uninitialized and nullable, but the parameter must be initialized within the method
The overloads of the CLR that allow methods to be made by using ref and out are as follows:
When called:
However, the following conditions are not allowed:
Read the "You must know. NET" 5.3 Parameters of the puzzle-the art of passing Page 167
Reading notes: The things that the parameters pass