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. 使用Reaver破解开启了WPS功能的wifi密码(wpa/wpa2)

    来自wikipeida: Wi-Fi保护设置(简称WPS,全称Wi-Fi Protected Setup)是一个无线网络安全标准,旨在让家庭用户使用无线网络时简化加密步骤.此标准由Wi-Fi联盟(Wi ...

  2. asp.net mvc获得请求体所有内容

    代码如下 Stream req = Request.InputStream; req.Seek(0, System.IO.SeekOrigin.Begin); string json = new St ...

  3. struts2逻辑视图类型汇总与解释(转)

    在struts2框架中,当action处理完之后,就应该向用户返回结果信息,该任务被分为两部分:结果类型和结果本身. 结果类型提供了返回给用户信息类型的实现细节.结果类型通常在Struts2中就已预定 ...

  4. Stars

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  5. CH1801 括号画家(栈模拟)

    描述 Candela是一名漫画家,她有一个奇特的爱好,就是在纸上画括号.这一天,刚刚起床的Candela画了一排括号序列,其中包含小括号( ).中括号[ ]和大括号{ },总长度为N.这排随意绘制的括 ...

  6. 对Tornado异步操作Sqlalchemy方法的选定 不错

    使用原因 在一个实时通讯的项目中,由于需要使用Websocket这一协议,便在Python框架中选定了Tornado,也同时使用了Sqlalchemy这一ORM框架. 大家都知道Tornado有异步非 ...

  7. openfaas 简单试用

    1. 安装 faas-cli  参考以前文章,或者使用官方的shell脚本   2. 简单例子 mkdir rong cd rong faas-cli new rong --lang python / ...

  8. php通过JavaBridge调用Java类库和不带包的自定义java类成功 但是调用带包的自定义Java类报错,该怎么解决

    php通过JavaBridge调用Java类库和不带包的自定义java类成功 但是调用带包的自定义Java类报错,Class.forName("com.mysql.jdbc.Driver&q ...

  9. 数据库中通过group by找出表中的重复数据

    有时候在做数据割接时会碰到数据插入失败的情况,大部分都是导出的数据中存在重复导致的.我们可以通过查询语句带分组条件来确认是否有重复数据.例如我现在有表 t_wlf_info,其中有个 username ...

  10. 关于yii2的中文乱码问题

    在数据库配置中这么配就可以了 <?php return [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=127.0.0. ...