C # Difficulty one by one (1): Ref parameter Pass

Source: Internet
Author: User
In general, the method's parameter pass is passed by value, that is, an object is passed as a parameter to the method, and the object is placed into the memory space of the pointer that the parameter object is in (described by C), that is, the object creates a copy at this location, and the copy is destroyed when the method finishes running. The use of this method of transmission takes up the overwhelming majority of routine methods of communication.

Another case is a reference pass, which differs from how the value is passed, that the object is passed to the method without affecting the method parameters, and that the method that is affected by the original parameter value is still returned, that is, MethodInstance (ref _refvalue) calls the method (ref _arg), but _ Refvalue has no effect on method, returning is still the result of _arg influence. It can also be thought that _arg must be assigned before use.

Example:

Another case is a reference pass, which differs from how the value is passed, that the object is passed to the method without affecting the method parameters, and that the method that is affected by the original parameter value is still returned, that is, MethodInstance (ref _refvalue) calls the method (ref _arg), but _ Refvalue has no effect on method, returning is still the result of _arg influence. It can also be thought that _arg must be assigned before use.

Example:

Using System; /****************************** * chapter:c# Difficult points (a) * Author: Wang Hongjian * date:2010-1-11 * blog:http://www.51obj.cn/* Email: Walkingp@126.com * Description: Emphasis on value delivery and reference delivery Method * ***************************/namespace Wang.testref {public class Normalclass {public void Shownormalresult (string name) {name = "Wang Hongjian"; Console.WriteLine (name); }} public class Refclass {//<summary>///reference type ref class///</summary>//<param name= "name" ></param& Gt public void Showrefresult (ref string name) {name = "Wang Hongjian"; Console.WriteLine (name); }} class Program {static void Main (string[] args) {string _name = "Zhou Runfa";//pass parameter #region Value pass parameter mode normalclass n = n EW Normalclass (); N.shownormalresult (_name);//Normal call #endregion #region Reference pass parameter mode refclass o = new Refclass (); O.showrefresult (ref _name);//The result is still a reference to pass the parameter Console.readkey (); #endregion}}}

Operation Result:


The above is the C # difficulty one by one (1): Ref parameter passed content, 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.