The pass value (by value) and the address (by reference) are ordinary pass-through parameter mode and the ref declaration mode, the method of communication requires the REF keyword decoration before use; Out is the output return pass, with ref the same place, The important difference is that the ref mentioned in the previous section has to be initialized in the referenced method, and out must have a return value in the referenced method.
Using System; /****************************** * chapter:c# Difficult points (a) * Author: Wang Hongjian * date:2010-1-15 * blog:http://www.51obj.cn/* Email: Walkingp@126.com * Description: Emphasis on output parameters out * ***************************/namespace Testout {class Program {//<summ Ary>///Out output value, return value///</summary>//<param name= "name" ></param>//<returns></ returns> static string Outresultmethod (out string name) {string _name = "Wang Hongjian"; name = _name; string __name = "Zheng Zi"; return __name; } static void Main (string[] args) {string _name = "Zhang Yu"; Console.WriteLine ("Before call: {0}", _name); String result= Outresultmethod (out _name); Console.WriteLine ("After call: {0}", _name); Console.WriteLine ("Return result: {0}", result); Console.readkey (); } } }
Operation Result:
Out is used more in actual projects, because out can return multiple values
Class Program {///<summary>///output value, return value///</summary>//<param name= "name" ></param>/// lt;returns></returns> static string Outresultmethod (out string name,out string password) {string _name = "Wang H Ongjian "; name = _name; String _password = "123456"; Password = _password; String __name = "Zheng Zi"; return __name; } static void Main (string[] args) {string _name = "Zhang Yu"; string _password; Console.WriteLine ("Before call: {0}", _name); string result = Outresultmethod (out _name, out _password);//Multiple Parameter Pass Console.WriteLine ("after call: {0}", _name); Console.WriteLine ("Return result: {0}", result); Console.readkey (); } }
Lenovo multiple parameters, you must have thought of using the array bar, good, the use of arrays is a good way, the array itself to the same type of parameter packaging "encapsulation" for an object to pass to the method, this can simplify the preparation of multi-parameter method, but also can achieve similar overloading effect.
static void Outarraymethod (out string[] strarr) {int i = 0; string[] _strarr = new STRING[10]; while (I < _strarr.leng TH) {_strarr[i] = "First" + i + "member"; i++;} Strarr = _strarr; } static void Main (string[] args) {string[] _strarr=new string[10]; Outarraymethod (out _strarr); foreach (String str in _strarr) {Console.WriteLine (str);} Console.readkey (); }
Operating effect:
The above is the C # difficulty one by one (2): Out the contents of the return parameters, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!