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. java svnkit实现svn提交,更新等操作

    官网:https://svnkit.com/ api:https://svnkit.com/javadoc/org/tmatesoft/svn/core/io/SVNRepository.html w ...

  2. 2017年国内已经开设机器人工程专业(080803T)高校名单

    相关资料来源于教育部公布的2014年度和2016年度普通高等院校本科专业备案或审批结果的通知: 2014年批次 http://www.moe.edu.cn/publicfiles/business/h ...

  3. struts2.5框架使用通配符指定方法,某一个匹配不到

    在学习struts框架时经常会使用到通配符调用方法,如下:但奇怪的是,在validateName请求老报404,其他的都是ok的,开始以为是配置错了,检查好久才知道: <action name= ...

  4. 如何将u盘、移动硬盘转化为活动分区--绝招

    https://jingyan.baidu.com/article/e75057f2a6a18aebc91a893e.html

  5. 帮你彻底解决eclipse(myeclipse)中写struts.xml配置文件

    其实,在自己写struts.xml的时候,竟然没有代码提示功能.让我非常的烦恼,其实解决这个问题的关键还是system不知道他的dtd的规则无法提示配置信息 很简单,那就让它知道就OK了!!! 道理明 ...

  6. Redis事件库源码分析

    由于老大在新项目中使用redis的事件库代替了libevent,我也趁着机会读了一遍redis的事件库代码,第一次读到“优美,让人愉快”的代码,加之用xmind制作的类图非常帅,所以留文纪念. Red ...

  7. 【openCV学习笔记】【3】高斯模糊一张图片(_cvSmooth相关编译错误)

    代码如下: #include <iostream> #include <opencv/highgui.h> #include <opencv/cv.h> void ...

  8. 如何在已经安装好的Nginx上增加新模块

    学习资源: https://blog.csdn.net/dxm2025/article/details/41149865 https://blog.csdn.net/qq_36663951/artic ...

  9. ORACLE常识

    1. ORACLE中查看表中的外键来源于哪些表 select cl.table_name from user_cons_columns cl left join user_constraints c ...

  10. 事务之二:spring事务(事务管理方式,事务5隔离级别,7个事务传播行为,spring事务回滚条件)

    事物管理对于企业应用来说是至关重要的,好使出现异常情况,它也可以保证数据的一致性. spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务管理使用TransactionTemplate或 ...