C# difficulties solved one by one (1): ref parameter passing

黄舟
Release: 2016-12-22 14:02:40
Original
1629 people have browsed it

Generally, the parameters of a method are passed by value, that is, an object is passed to the method as a parameter, and the object is stored in the memory space of the pointer where the parameter object is located (described using C), that is, the object is in A copy is created at this location, which will be destroyed when the method ends; the use of this transfer method accounts for the vast majority of daily method parameter transfers.

Another situation is reference passing, which is different from value passing. When the object is passed to a method, it has no effect on the method parameters. What is still returned is the method affected by the original parameter value, that is, MethodInstance(ref _refValue) calls Method (ref _arg) method, but _refValue has no effect on Method, and the result affected by _arg is still returned. It can also be thought of that _arg must be assigned before use.

Example:

Another situation is reference passing, which is different from value passing. When the object is passed to a method, it has no effect on the method parameters. What is still returned is the method affected by the original parameter value, that is, MethodInstance(ref _refValue) calls the Method (ref _arg) method, but _refValue has no effect on Method, and the result affected by _arg is still returned. It can also be thought of that _arg must be assigned before use.

Example:

using System; /****************************** * Chapter:C#难点逐个击破(一) * Author:王洪剑 * Date:2010-1-11 * Blog:http://www.51obj.cn/ * Email:walkingp@126.com * Description:重点讲解值传递方式与引用传递方式 * ***************************/ namespace Wang.TestRef { public class NormalClass { public void ShowNormalResult(string name) { name = "Wang Hongjian"; Console.WriteLine(name); } } public class RefClass { ///  /// 引用类型ref类 ///  ///  public void ShowRefResult(ref string name) { name = "Wang Hongjian"; Console.WriteLine(name); } } class Program { static void Main(string[] args) { string _name = "Zhou Runfa";//传递参数 #region 值传递参数方式 NormalClass n = new NormalClass(); n.ShowNormalResult(_name);//正常调用 #endregion #region 引用传递参数方式 RefClass o = new RefClass(); o.ShowRefResult(ref _name);//结果仍然为引用传递参数 Console.ReadKey(); #endregion } } }
Copy after login

Operating results:

C# difficulties solved one by one (1): ref parameter passing


The above are the C# difficulties one by one (1): the content passed by the ref parameter. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com) !


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!