• 引用类型的变量不直接包含其数据;它包含的是对其数据的引用。 当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值。 但是无法更改引用本身的值;也就是说,不能使用相同的引用为新类分配内存并使之在块外保持。 若要这样做,应使用 ref 或 out 关键字传递参数。
  • 通过ref允许调用方法来修改引用参数引用的对象。 对象的存储位置传递给方法作为引用参数的值。 如果您修改了更改参数的存储位置,那么您也更改了基础参数的存储位置。

下面的示例演示通过值向 Change 方法传递引用类型的参数 arr。 由于该参数是对 arr 的引用,所以有可能更改数组元素的值。 但是,尝试将参数重新分配到不同的内存位置时,该操作仅在方法内有效,并不影响原始变量 arr。

class PassingRefByVal
{
static void Change(int[] pArray)
{
pArray[0] = 888; // This change affects the original element.
pArray = new int[5] {-3, -1, -2, -3, -4}; // This change is local.
System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
} static void Main()
{
int[] arr = {1, 4, 5};
System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]); Change(arr);
System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]);
}
}
/* Output:
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: 888
*/

在上个示例中,数组 arr 为引用类型,在未使用 ref 参数的情况下传递给方法。 在此情况下,将向方法传递指向 arr 的引用的一个副本。 输出显示方法有可能更改数组元素的内容,在这种情况下,从 1 改为 888。 但是,在Change 方法内使用 new 运算符来分配新的内存部分,将使变量 pArray 引用新的数组。 因此,这之后的任何更改都不会影响原始数组 arr(它是在 Main 内创建的)。 实际上,本示例中创建了两个数组,一个在 Main 内,一个在Change 方法内。

下面的示例与前面的示例,除此之外, ref 关键字添加到方法标头并调用。 在方法影响出现在调用过程的原始变量的任何更改。

class PassingRefByRef
{
static void Change(ref int[] pArray)
{
// Both of the following changes will affect the original variables:
pArray[0] = 888;
pArray = new int[5] {-3, -1, -2, -3, -4};
System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
} static void Main()
{
int[] arr = {1, 4, 5};
System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]); Change(ref arr);
System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);
}
}
/* Output:
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: -3
*/

方法内发生的所有更改都影响 Main 中的原始数组。 实际上,使用 new 运算符对原始数组进行了重新分配。 因此,调用 Change 方法后,对 arr 的任何引用都将指向 Change 方法中创建的五个元素的数组。

交换字符串是通过引用传递引用类型参数的很好的示例。 本示例中,str1 和 str2 两个字符串在 Main 中初始化,并作为由 ref 关键字修改的参数传递给 SwapStrings 方法。 这两个字符串在该方法内以及 Main 内均进行交换。

 class SwappingStrings
{
static void SwapStrings(ref string s1, ref string s2)
// The string parameter is passed by reference.
// Any changes on parameters will affect the original variables.
{
string temp = s1;
s1 = s2;
s2 = temp;
System.Console.WriteLine("Inside the method: {0} {1}", s1, s2);
} static void Main()
{
string str1 = "John";
string str2 = "Smith";
System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2); SwapStrings(ref str1, ref str2); // Passing strings by reference
System.Console.WriteLine("Inside Main, after swapping: {0} {1}", str1, str2);
}
}
/* Output:
Inside Main, before swapping: John Smith
Inside the method: Smith John
Inside Main, after swapping: Smith John
*/

本示例中,需要通过引用传递参数以影响调用程序中的变量。 如果同时从方法头和方法调用中移除 ref 关键字,则调用程序中不会发生任何更改。

传递引用类型参数(ref)的更多相关文章

  1. 传递引用类型参数的两种方式(转自 MSDN)

    引用类型的变量不直接包含其数据:它包含的是对其数据的引用.当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值(更改属性的值),但是无法更改引用本身的值:也就是说,不能使用相同的引 ...

  2. javascript 传递引用类型参数

    JavaScript代码如下: function setName(obj){ obj.name = "test1"; obj = new Object(); obj.name = ...

  3. c# 方法参数(传值,传引用,ref,out,params,可选参数,命名参数)

       一.方法参数的类型----值类型和引用类型 当方法传递的参数是值类型时,变量的栈数据会完整地复制到目标参数中即实参和形参中的数据相同但存放在内存的不同位置.所以,在目标方法中对形参所做的更改不会 ...

  4. c# 值传递 引用传递

    以前一直误以为引用类型,在作为参数传递时,都是引用传递(类似于值传递中的ref),也就是说,把引用类型的变量作为参数传递给方法,在方法中修改该参数,会改变这个变量的值, 后来通过一些事例发现,上面的认 ...

  5. 给方法传递参数:ref参数和out参数

    /*--------------------------------------------------- 给方法传递参数:ref参数和out参数 (P106) ------------------- ...

  6. thread - 传递引用参数

    当给 thread 的执行函数传递指针参数时,没有任何问题,但是如果想传递引用,按照普通函数的调用方法会遇到编译失败: #include <iostream> #include <t ...

  7. Android学习笔记_23_服务Service之AIDL和远程服务实现进程通信以及进程间传递自定义类型参数

    一.了解AIDL语言: 在Android中, 每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢? 显然, Java中是不支持跨进程内存共享的.因此要传递对象, 需要把对象解析 ...

  8. 《从零开始学Swift》学习笔记(Day 20)——函数中参数的传递引用

    原创文章,欢迎转载.转载请注明:关东升的博客 参数的传递引用 类是引用类型,其他的数据类型如整型.浮点型.布尔型.字符.字符串.元组.集合.枚举和结构体全部是值类型. 有的时候就是要将一个值类型参数以 ...

  9. JAVA一个关于传递引用的测试

    以下测试主要为了说明:对传递对象或传递引用进行修改,对最终值的影响情况 public class PassTest {     @Before     public void setUp() thro ...

随机推荐

  1. Android将Activity 打 jar包 (解决资源文件不能打包的问题)

    转载地址:http://blog.csdn.net/xiaanming/article/details/9257853 最近有一个需要,我们公司做了一个apk客户端,然后其他的公司可以根据自己的需要来 ...

  2. The listener supports no services解决一例

    The listener supports no services解决一例   Listener动态监听静态监听注册实例 今天做Advacned Replication实验的时候碰到一个问题,启动目标 ...

  3. C# 自定义序列化问题

        public class overdue     {         public int overdueTimes { get; set; }         /// <summary ...

  4. ajax跨域请求带cookie

    调用网站:a.xxx.com jQuery(document).ready(function () { $.ajax({ type: "get", async: true, url ...

  5. CSS盒子模型学习记录3(侧面导航栏)

    学习http://www.blueidea.com/tech/web/2007/4545_2.asp 代码试验 html <!DOCTYPE html PUBLIC "-//W3C// ...

  6. dubbox新特性介绍

    dubbx是当当网对原阿里dubbo2.x的升级,并且兼容原有的dubbox.其中升级了zookeeper和spring版本,并且支持restfull风格的远程调用. dubbox git地址:  h ...

  7. docker-compose安装使用

    Docker Compose的工作原理 Docker Compose将所管理的容器分为三层,工程(project),服务(service)以及容器(contaienr).Docker Compose运 ...

  8. WebForm---增删改(内置对象)

    一.添加 前台代码: <body> <form id="form1" runat="server"> <h1>用户添加< ...

  9. memory allocation

    1 malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符. 2,malloc, 必须 包含头文件<stdlib.h> 3, nt* p_scalar = ...

  10. Razor引擎中的_ViewStart.cshtml

    Startup Code是在所有View执行之前加载和执行的代码. 在Razor引擎中的_ViewStart.cshtml 就是装载这些“预执行代码”的文件,它有两个特点: 一.就是所有View执行之 ...