Http://www.cnblogs.com/mdnx/archive/2012/09/04/2671060.html
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceconsoleapplication1{classProgram {/*Let's take a look at what an argument is and what a formal parameter is. The so-called formal parameters are actually the arguments that are taken when the function is defined. For example, the static void myfuntion (int i, int t) where int i and int t are formal parameters. * and the actual argument? The arguments represent the two arguments that will be passed into the function, either a variable or a constant. As long as the parameter type is the same*/ //The function is divided into two ways: transfer value and address. The value of the pass does not affect the data of the argument, and the address affects the data of the argument. Here are a few pieces of code to sample//Such a rule should be followed in the selection of the use of a value or a transmission. //1. The pass value is a copy of the contents of the formal parameter to the argument. //2. A pass-through is a form that participates in an argument and points to an address. //If the data is used in a large number of ways will inevitably affect the performance of the program, so when the data is large, you can use the way to address. The way you use the address will greatly improve the efficiency of your program. Static voidMyfuntion (intIintT//This function will run as a pass-through value. { if(I >t) I+=5; ElseT+=5; } Static voidMyfuntion1 (ref intIref intT//This function will be performed in the way of the address. Because the REF keyword was added earlier, the function simply adds the REF keyword to the operation as a pass-through method. { if(I >t) I+=5; ElseT+=5; } Static voidMyfuntion2 (int[] Array)//Note here that this function does not have a ref keyword, but it is also used as a pass-through operation. Because in C #, reference types are all operands, and array is an array, and the array belongs to the reference type, so .... { for(inti =0; I < array. Length; i++) {Array[i]= +; } } Static voidMyfuntion3 (intIintT out intS//This function, although the return value is void (which means there is no return value), actually has a return value because we added a new keyword, out, that the keyword can return s alone. The same out keyword is also performed in the way of the address. Also, out parameters can be used without assigning values, even if you are assigned a value that is ignored{s= -; if(I >t) I+=5; ElseT+=5; } Static voidMain (string[] args) { //Now let's use the four functions defined above to see what the effect is. intA =Ten, B = the, C; //1. Functions of the value-passing methodMyfuntion (A, b);//at this point A and B are still equal to 10 and 15, because the parameters in the function change the result. But it does not affect the argument.Console.WriteLine ("a: {0} and B: {1}", A, B +"\ n"); //2. Function of the method of transmitting the addressMyfuntion1 (refArefb);//Note here that the REF keyword is used in the function, and the REF keyword is used when calling. The result should be a = ten B = 20 because the above code is the address method, and the parameter changes will affect the argumentConsole.WriteLine ("a: {0} and B: {1}", A, B +"\ n"); //3. Implicit address mode function. int[] My =New int[5];//define an array and assign it a value for(inti =0; i < my.length; i++) {My[i]= -; } //Call function NowMyfuntion2 (My);//as you can imagine, every bit of my will be equal to foreach(intWinchMy) {Console.WriteLine (w); Console.WriteLine (); } //4. Out parameter functionMyfuntion3 (A, B, outc);//Also , as with ref, you need to add a keyword when callingConsole.WriteLine (c);//c should be equal to//let's try to assign the C value and then call the function to see what happens.c = -; Myfuntion3 (A, B, outc); Console.WriteLine (c); //or will be equal to; } }}//here is the result of the Operation/*a:10 and B:15-------------------a:10 and b:20------------------4040404040--------------------------------- -----------**/
C # function Transfer value and address (GO)