• 引用类型的变量不直接包含其数据;它包含的是对其数据的引用。 当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值。 但是无法更改引用本身的值;也就是说,不能使用相同的引用为新类分配内存并使之在块外保持。 若要这样做,应使用 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. 关于view.measure

    在编写下啦刷新的项目代码的时候,在Listview的HeaderView中的head.xml文件中,根布局为RelativeLayout的时候,在计算headerView.measure的时候,出现空 ...

  2. QueryRunner(common-dbutils.jar)

    QueryRunner update方法:* int update(String sql, Object... params) --> 可执行增.删.改语句* int update(Connec ...

  3. <a>标签的href和onclick属性

    讨论 <a>标签中,href和onclick事件的顺序与冲突问题. 首先明确一点:链接的onclick 事件被先执行,其次是href属性下的动作(页面跳转,或 javascript 伪链接 ...

  4. java中Thread的 interrupt异常处理

    http://blog.csdn.net/srzhz/article/details/6804756

  5. HTML、CSS和JS

    一.html 1.web流程中的HTML HTML---->赤裸裸的人 CSS  ---->穿华丽的衣服 JS    ---->让人动起来 浏览器和server端之间的通信本质上是字 ...

  6. 64 位 Ubuntu 下 android adb 不可用解决方法

    解决方案: 安装ia32-libs 在终端执行 sudo apt-get install ia32-libs 其间会提示所依赖的某些包不存在,直接 sudo apt-get 安装即可.

  7. C#去掉list集合中的重复数据

    List<string> conList= new List<string>(); List<string> listII = new List<string ...

  8. VC++模态对话框和非模态对话框

    MFC中有两种类型的对话框:模态对话框和非模态对话框.  模态对话框是指当其显示时,程序会暂停执行,直到关闭这个模态对话框后,才能继续执行程序中其他任务.非模态对话框是指当其显示时,允许转而执行程序中 ...

  9. linux 下tomcat catalina.out日志操作

    1. 查看日志 tail -f catalina.out 会动态打印日志. 2. 查看所有日志 less -f catalina.out 打开所有日志后,默认是显示第一页,常用命令用到如下: G:到达 ...

  10. Spring —— 三种配置数据源的方式:spring内置、c3p0、dbcp

    01.Spring内置数据源配置Class:DriverManagerDataSource全限定名:org.springframework.jdbc.datasource.DriverManagerD ...