Parameter passing mechanism of six methods value parameter, reference parameter, output parameter
//transfer mechanism of parametersusingSystem;classmethod{//value parameter, passing the value itself, without altering the values of the external variable Public Static voidValuemethod (inti) {i++; } //reference parameters, pass the data address, directly manipulate the data, the original value to change//Note that the value of string is not changed after the assignment. Public Static voidReferencemethod (ref inti) {i++; } //output parameters, the delivery is also the address, but the difference is that the operation must assign the initial value to the variable Public Static voidOutputmethod ( out inti) {i=0;//assigning an initial valuei++; } Static voidMain () {//test three kinds of transmission value methods intI=0; Valuemethod (i); Console.WriteLine ("i="+i);//output is 0, original value unchanged intj=0; Referencemethod (refj); Console.WriteLine ("j="+J);//The output is 1, the original value is +1; intK; Outputmethod ( outk); Console.WriteLine ("k="+K);//The assigned initial value is 0 and the output is 1. }}
Variable number of parameters
To declare a method as a method that can accept a variable number of parameters, use the params keyword
/** Created by SharpDevelop. * User:yangq * DATE:2016/8/27 * time:23:57 * ~ Change this template use Tools | Options | Coding | Edit Standard Headers. */usingSystem;classmethod{Static intAddi (params int[] values)//the number of parameters can vary, stored as an array{//the parameters are written as (1) or (1,2,3,4,5)//method is to add all the parameters, return and value intsum=0; foreach(intIinchvalues) sum+=i; returnsum; } Static voidMain () {Console.WriteLine (Addi (1,2,3,4,5));//the output is the result of the. }}
The parameter passing mechanism of the method (C #)