C # has three keywords-Ref, out, And Params. Although I do not like these three keywords, they are suspected to damage the object-oriented feature. But since M $ is integrated into the C # system, let's take a look at the parameter modifiers ref, out, Params, and their differences. No.1 Params A keyword that allows a method (function) to have variable parameters. Principle: no other parameters are allowed after the Params keyword in the method declaration, and only one Params keyword is allowed in the method declaration. Example (copy to vs2005 for use, which is not described below) Public partial class form1: Form { Public static void useparams (Params int [] list) { String temp = ""; For (INT I = 0; I <list. length; I ++) Temp = temp + "" + list [I]. tostring (); MessageBox. Show (temp ); } Public static void useparams2 (Params object [] list) { String temp = ""; For (INT I = 0; I <list. length; I ++) Temp = temp + "" + list [I]. tostring (); MessageBox. Show (temp ); } Public form1 () { Initializecomponent (); } private void button#click (Object sender, eventargs e) {< br> useparams (1, 2, 3 ); // check that the parameters are 3 useparams (1, 2); // check that the parameters are 2. Variable useparams2 (1, 'A ', "test"); int [] myarray = new int [3] {10, 11, 12}; useparams (myarray ); // It can also be a container class, variable :) }< BR >} No. 2 out This is a reference transfer L. Principle 1: When a method (function) uses out as a parameter, any changes made to the out parameter in the method (function) are reflected in this variable. Principle 2: It is very useful to declare the out method when you want the method to return multiple values. You can still return a value using the out parameter. A method can have more than one out parameter. Principle 3: To use the out parameter, the parameter must be passed to the method as the out parameter explicitly. The value of the out parameter is not passed to the out parameter. Principle 4: you do not need to initialize the variable passed as the out parameter, because the out parameter clears itself when it enters the method (function) and turns itself into a clean parameter, for this reason, the out parameter must be assigned a value before the method is returned.. net ). Principle 5: attributes are not variables and cannot be passed as out parameters. Principle 6: if the declaration of the two methods is only different from that of the out method, the overload will occur. However, it is not possible to define a unique reload for ref and out. For example, the following overload statement is valid: class myclass {< br> Public void mymethod (int I) {I = 10 ;} Public void mymethod (Out int I) {I = 10 ;}< BR >}< br> The following overload declaration is invalid: class myclass {< br> Public void mymethod (Out int I) {I = 10 ;}< br> Public void mymethod (ref int I) {I = 10 ;}< BR >}< br> for information about passing arrays, see passing Arrays Using ref and out. attached example No. 3 ref Ref is just an address !!! Principle 1: When a method (function) uses ref as a parameter, any changes made to the ref parameter in the method (function) will be reflected in the variable. Principle 2: When a method is called, any changes made to the parameters in the method will be reflected in this variable. Principle 3: To use the ref parameter, you must pass the parameter as the ref parameter to the method explicitly. The ref parameter value can be passed to the ref parameter. Principle 4: The variables passed by the ref parameter must be initialized, because the ref parameter still points to the original value after it enters the method (function, for this reason, the ref parameter can also be used internally without any operation. Principle 6: if the declarations of the two methods are only different in their use of ref, the overload will occur. However, it is not possible to define a unique reload for ref and out. For example, the following overload declaration is valid: Class myclass { Public void mymethod (int I) {I = 10 ;} Public void mymethod (ref int I) {I = 10 ;} } However, the following overload statement is invalid: Class myclass { Public void mymethod (Out int I) {I = 10 ;} Public void mymethod (ref int I) {I = 10 ;} } For information on passing arrays, see passing Arrays Using ref and out. Example Public static string testout (out string I) { I = "out B "; Return "Return Value "; } Public static void testref (ref string I) { // Change the Parameter I = "Ref B "; } Public static void testnoref (string refi) { // No need to change anything. This is too obvious. Refi = "on C "; } Public form1 () { Initializecomponent (); } Private void button#click (Object sender, eventargs E) { String Outi; // initialization not required MessageBox. Show (testout (Out Outi); // Return Value // Output "Return Value "; MessageBox. Show (Outi); // The out parameter after the call // Output "out B "; string refi = "A"; // required testref (ref refi); // call the parameter MessageBox. show (refi); // output "Ref B"; testnoref (refi); // do not use ref MessageBox. show (refi); // output "Ref B"; |