ref & out - C#中的参数传递

  ref与out均指定函数参数按引用传递,惟一的不同是,ref传递的参数必须初始化,而out可以不用。

  ref与out无法作为重载的依据,即ref与out编译器认为一样。如下:

  

  但是ref函数与非ref函数是可以重载的,如下:

  

  To use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword, as shown in the following example.

  

  A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref or out keyword.

class PassingRefByVal
{
static void Change(int[] pArray)
{
pArray[] = ; // This change affects the original element.
pArray = new int[] {-, -, -, -, -}; // This change is local.
System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[]);
} static void Main()
{
int[] arr = {, , };
System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr []); Change(arr);
System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr []);
}
}
/* 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
*/

参考:

1、http://msdn.microsoft.com/zh-cn/library/14akc2c7(VS.80).aspx

2、http://msdn.microsoft.com/zh-cn/library/t3c3bfhx(v=vs.80).aspx

3、http://msdn.microsoft.com/zh-cn/library/s6938f28.aspx

ref & out - C#中的参数传递的更多相关文章

  1. SQL实现递归及存储过程中In()参数传递解决方案[转]

    SQL实现递归及存储过程中In()参数传递解决方案   1.SQL递归 在SQL Server中,我们可以利用表表达式来实现递归算法,一般用于阻止机构的加载及相关性处理.   -->实现: 假设 ...

  2. SQL实现递归及存储过程中 In() 参数传递解决方案

    1.SQL递归 在SQL Server中,我们可以利用表表达式来实现递归算法,一般用于阻止机构的加载及相关性处理. -->实现: 假设OrganiseUnit(组织机构表)中主要的三个字段为Or ...

  3. php 链接中加参数传递

    原文:php 链接中加参数传递 php链接中加参数是在源链接中加"?",问号之后就可以跟参数列表,para1=value1&para2=value2&para3=v ...

  4. java中的参数传递是按引用传递还是按值传递

    最近去面试,有一个面试官问到java中参数传递的问题,感觉自己对于这一块还是理解的不够深.今天我们就一起来学习一下Java中的接口和抽象类.下面是本文的目录大纲: 一 . 什么是按值传递,什么是按引用 ...

  5. python中的参数传递和返回值

    python中的参数传递类似java,有着自己的内存回收机制,这和C++有着很大的差别. 1.函数的参数传递: >>> a = [, , ] >>> def fun ...

  6. Python中函数参数传递问题【转】

    1. Python passes everything the same way, but calling it "by value" or "by reference& ...

  7. C语言中函数参数传递

    C语言中函数参数传递的三种方式 (1)值传递,就是把你的变量的值传递给函数的形式参数,实际就是用变量的值来新生成一个形式参数,因而在函数里对形参的改变不会影响到函数外的变量的值.(2)地址传递,就是把 ...

  8. Python中的参数传递问题

    首先需要说明python中元组,列表,字典的区别. 列表: 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单. 例如:lst = ['arwen',123] 向list中添加项有两种方法:ap ...

  9. ECMAScript中所有参数传递的都是值,不可能通过引用传递参数

    今天在看JavaScript高级程序设计(第三版)时,看到了这个问题:ECMAScript中所有参数传递的都是值,不可能通过引用传递参数. 在我的印象中,其他语言比如Java,C++等,传递参数有两种 ...

随机推荐

  1. 《Drools7.0.0.Final规则引擎教程》第4章 4.2 auto-focus

    auto-focus 在agenda-group章节,我们知道想要让AgendaGroup下的规则被执行,需要在代码中显式的设置group获得焦点.而此属性可配合agenda-group使用,代替代码 ...

  2. 看过自会理解, Photon Server 常见概念分析.

    http://stackoverflow.com/questions/10823915/photon-server-newbie-questions/11653419#11653419 Channel ...

  3. 微信小程序-富文本解析插件wxParse基础使用及问题解决

    一.插件准备 在github上可以直接下载该插件:https://github.com/icindy/wxParse 二.基本使用 1.将插件导入项目: 将wxParse文件夹放在项目目录下,如图: ...

  4. 伪元素:focus-within

    1.盒子边框的线条动画: <div class="cont"> <div class="bb"></div> </di ...

  5. ehcache缓存技术的特性

    Ehcache是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从hibernate的缓存开始的.网上中文的EhCache材料以简单介绍和配置方法居多,如果你有这方面的 ...

  6. python 爬虫的一些使用技巧

    1.最基本的抓站 import urllib2 content = urllib2.urlopen('http://XXXX').read() -2.使用代理服务器这在某些情况下比较有用,比如IP被封 ...

  7. oracle fn project 开源faas 框架

    1. 介绍 Fn is an event-driven, open source, functions-as-a-service compute platform that you can run a ...

  8. dede数据库类使用方法 $dsql(转)

    dede数据库类使用方法 $dsql   dedecms的数据库操作类,非常实用,在二次开发中尤其重要,这个数据库操作类说明算是奉献给大家的小礼物了. 引入common.inc.php文件 ? 1 r ...

  9. ORMLite的使用

    首先需要下载core和android两个jar http://ormlite.com/releases/ 然后拷贝到libs文件夹下并且添加为库 具体的使用看标程: School.java packa ...

  10. 使用ajax提交form表单,formData

    http://www.cnblogs.com/zhuxiaojie/p/4783939.html formDatahttps://developer.mozilla.org/zh-CN/docs/We ...