There are four types of parameters: Passing parameters by value, passing parameters by reference (REF), output parameters (out), array parameters (parameter array, Params)
1. PASS Parameters by value: ① A value parameter is passed to a method by assigning the value of a real parameter to the form parameter. ② In a value parameter, the real parameter can also be an expression that meets the type requirements of any calculation result, not necessarily the format of the variable. Code①:// As long as the parameter is an expression that meets the Method Type
Class Program { Static Void Main ( String [] ARGs) {program Program = New Program (); Int X = 10 ; Int Y = 20 ; Int Sum = program. getsum (x + y, y * x/ 2 ); // Here, X + Y and so on } Public Int Getsum ( Int X, Int Y ){ Return X + Y ;}}
2. PASS Parameters by reference: ref In response to the "pass by value" mentioned in previous 1, the parameter is referenced rather than the parameter value. If the parameter has been assigned a value in the method, the parameter value will not be changed after the method is executed. Code ②:
Class Program { Static Void Main (String [] ARGs) {program PG = New Program (); Int X = 10 ; Int Y = 20 ; Pg. getvalue ( Ref X, Ref Y); console. writeline ( " X = {0}, y = {1} " , X, y); console. Readline ();} Public Void Getvalue ( Ref Int X, Ref Int Y) {x = 521 ; Y = 520 ;}}
The running result is: Code ③:
Class Program { Static Void Main ( String [] ARGs) {program Program = New Program (); Int X = 10 ; Int Y = 20 ; Program. Exchange (x, y); console. writeline ( " X = {0}, y = {1} " , X, y); console. Readline ();} Public Void Exchange ( Int X, Int Y ){ Int Temp; temp = X; x =Y; y = Temp ;}}
The running result is as follows: we can see that the expected results are not implemented, and the values of X and Y are interchangeable. However, adding the ref keyword will achieve the desired result: Code 4:
Class Program { Static Void Main ( String [] ARGs) {program Program = New Program (); Int X =10 ; Int Y = 20 ; Program. Exchange ( Ref X, Ref Y); console. writeline ( " X = {0}, y = {1} " , X, y); console. Readline ();} Public Void Exchange (Ref Int X, Ref Int Y ){ Int Temp; temp = X; x = Y; y = Temp ;}}
The running result is:, which also indicates that ref is a parameter passed by reference.
3. output parameter: Out (the difference between ref and out will be discussed in the next blog) First try the Code: Code ⑤:
Class Program { Static Void Main ( String [] ARGs) {program PG = New Program (); Int X = 10 ; Int Y = 20 ; Pg. getvalue ( Ref X, Out Y); console. writeline ( " X = {0}, y = {1} " , X, y); console. Readline ();} Public Void Getvalue ( Ref Int X, Out Int Y ){ // Console. writeline (X. tostring ()); // Console. writeline (Y. tostring ()); X = 521 ; Y = 520 ;}
The running result is as follows: It can be seen that the results obtained by using ref and out are the same. However, if the comments are removed separately, the following error occurs when the comments of the 2nd rows are removed: The out parameter "Y" is not assigned ". Note: 1. the compiler allows you to read the value of referenced parameters at any point in the method; 2. the compiler prohibits reading the output parameter before assigning a value; 3. the initial value of the output parameter is basically meaningless because it is assigned as a new value before use. (It can be understood as follows:Console. writeline (Y. tostring (); the Code is written inY = 520; this statement can be run correctly and the running result is 520; X = 521, y = 520) 4. Parameter array (Params) This is a bit like an "optional parameter. The Code is as follows: Code 6:
1 Class Program 2 { 3 Static Void Main ( String [] ARGs) 4 { 5 Program Pg = New Program (); 6 Pg. dosomething ( " A " ); 7 Pg. dosomething ( " A " , 1 , 2 ); 8 Pg. dosomething ( " A " , 1 , 2 ,3 ); 9 Console. Readline (); 10 11 } 12 13 Public Void Dosomething ( String STR, Params Int [] Values) 14 { 15 If ( Null ! = Values & values. length> 0 ) 16 { 17 For ( Int I = 0 ; I <values. length; I ++ ) 18 { 19 Console. writeline ( " {0}, {1} " , STR, values [I]); 20 } 21 } 22 Else 23 { 24 Console. writeline (STR ); 25 } 26 } 27 }
The running result is as follows: