ref & out - C#中的参数传递
【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#中的参数传递的更多相关文章
- SQL实现递归及存储过程中In()参数传递解决方案[转]
SQL实现递归及存储过程中In()参数传递解决方案 1.SQL递归 在SQL Server中,我们可以利用表表达式来实现递归算法,一般用于阻止机构的加载及相关性处理. -->实现: 假设 ...
- SQL实现递归及存储过程中 In() 参数传递解决方案
1.SQL递归 在SQL Server中,我们可以利用表表达式来实现递归算法,一般用于阻止机构的加载及相关性处理. -->实现: 假设OrganiseUnit(组织机构表)中主要的三个字段为Or ...
- php 链接中加参数传递
原文:php 链接中加参数传递 php链接中加参数是在源链接中加"?",问号之后就可以跟参数列表,para1=value1¶2=value2¶3=v ...
- java中的参数传递是按引用传递还是按值传递
最近去面试,有一个面试官问到java中参数传递的问题,感觉自己对于这一块还是理解的不够深.今天我们就一起来学习一下Java中的接口和抽象类.下面是本文的目录大纲: 一 . 什么是按值传递,什么是按引用 ...
- python中的参数传递和返回值
python中的参数传递类似java,有着自己的内存回收机制,这和C++有着很大的差别. 1.函数的参数传递: >>> a = [, , ] >>> def fun ...
- Python中函数参数传递问题【转】
1. Python passes everything the same way, but calling it "by value" or "by reference& ...
- C语言中函数参数传递
C语言中函数参数传递的三种方式 (1)值传递,就是把你的变量的值传递给函数的形式参数,实际就是用变量的值来新生成一个形式参数,因而在函数里对形参的改变不会影响到函数外的变量的值.(2)地址传递,就是把 ...
- Python中的参数传递问题
首先需要说明python中元组,列表,字典的区别. 列表: 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单. 例如:lst = ['arwen',123] 向list中添加项有两种方法:ap ...
- ECMAScript中所有参数传递的都是值,不可能通过引用传递参数
今天在看JavaScript高级程序设计(第三版)时,看到了这个问题:ECMAScript中所有参数传递的都是值,不可能通过引用传递参数. 在我的印象中,其他语言比如Java,C++等,传递参数有两种 ...
随机推荐
- 【SQL查询】视图_view
转自:http://database.e800.com.cn/articles/2009/719/1248015564465_1.html 视图是从一个或几个基本表(或视图)导出的表.它与基本表不同, ...
- 压缩JS,提高代码执行速度
压缩JS java -jar yuicompressor-2.4.jar --type js xxx.js -o xxx.js --charset utf-8 压缩CSS java -jar yuic ...
- win7下破解无线网密码
很多朋友喜欢做一些比较有成就感的事情,例如破解别人的密码,现在破解的方式大部分还是以跑字典这种没有任何技术含量的手段进行,那么破解的成功与否就和我们的字典有很多的关系了,本次经验就来教大家怎样进行字典 ...
- C# null和" "的区别
String str1 = null; str引用为空 String str2 = ""; str引用一个空串 也就是null没有分配空间,""分 ...
- jQuery的ajax跨域实现
今天有人问我跨域ajax请求是否可以发送,之前没接触过此类问题,没答上,后来查了下,以下备忘. 我在本地建了三个站点,并设置了host文件模拟跨子域和跨全域 coolkissbh.com blog.c ...
- VBA程序的调试
VBA程序的调试:设置断点.单步跟踪.设置监视窗 Acces的VBE编程环境提供了完整的一套调试工具和调试方法.熟练掌握好这些调试工具和调试方法的使用,可以快速.准确地找到问题所在,不断修改,加以完善 ...
- (转)java中查找List的快捷小方法
相信java开发的程序员,经常会遍历list里的数据,来查找想要的数据.之后选出来在做处理,我这有个小方法在大量数据的情况下能更快捷,当然这方法只对菜鸟有点用,对老鸟来说也许有更好的方法,请指点 遍历 ...
- 关联数组的错误,mysql insert varchar 原生的错误
在写代码的时候,没注意犯了2个低级错误: 关联数组的错误 $array = ['id' => '03657', 'kf_phone ' => 18796442]; 然后你再读取的时候就需要 ...
- Oracle中遇到的错误
1. ORA-00937: 不是单组分组函数 和 不是group by表达式 --select count(corp_tn),state_code from t_oa_main where cor ...
- HTMLTestRunner 汉化版---来源一个大神的源码(加了失败截图,用例失败重新执行 功能)
HTMLTestRunner 汉化版 20170925 测试报告完全汉化,包括错误日志的中文处理 针对selenium UI测试增加失败自动截图功能 增加失败自动重试功能 增加饼图统计 同时兼容pyt ...