C # Difficulty one by one (2): Out return parameter

Source: Internet
Author: User
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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.