C # Pain-breaking (1): Ref parameter pass _c# Tutorial

Source: Internet
Author: User
In general, the parameter passing of a method is passed by value. That is, an object is passed as a parameter to the method usage, and the object is positioned in the memory space of the pointer to the Parameter object (described by C), where the object creates a copy, and the copy is destroyed when the method runs at the end. The use of this method of transmission occupies the vast majority of the daily methods of communication.

The other case is a reference pass, which differs from the way the value is passed, when the object passed to the method has no effect on the method parameter, and still returns the method that is affected by the value of the original parameter, that is, MethodInstance (ref _refvalue) calls method (ref _arg), but _ Refvalue has no effect on method, return is still the result of _arg effect. This can also be thought of _arg must be assigned before use.

Example:

The other case is a reference pass, which differs from the way the value is passed, when the object passed to the method has no effect on the method parameter, and still returns the method that is affected by the value of the original parameter, that is, MethodInstance (ref _refvalue) calls method (ref _arg), but _ Refvalue has no effect on method, return is still the result of _arg effect. This can also be thought of _arg must be assigned before use.

Example:
Copy Code code as follows:

Using System;

/******************************
* chapter:c# Difficult to break (i)
* Author: Wang Hongjian
* DATE:2010-1-11
* blog:http://www.51obj.cn/
* email:walkingp@126.com
* Description: Emphasis on the transfer of value and the way of reference
* ***************************/
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>
public void Showrefresult (ref string name)
{
Name = "Wang Hongjian";
Console.WriteLine (name);
}
}
Class Program
{
static void Main (string[] args)
{
string _name = "Zhou Runfa";//Pass Parameters

#region Value Pass Parameter method
Normalclass n = new Normalclass ();
N.shownormalresult (_name);/Normal Call
#endregion

#region Reference Pass Parameter method
Refclass o = new Refclass ();
O.showrefresult (ref _name);//The result is still a reference pass parameter
Console.readkey ();
#endregion
}
}
}

Run Result:

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.