您的位置:首页 > 编程语言 > C#

C#中ref和out的区别

2009-07-29 14:33 253 查看
看了很多.按照 自己明白解释一下

.NET Ref和Out关键字

对于值类型。

如果不使用ref /out则传递的只是这些值的COPY,

使用了Ref和Out的效果就几乎和C中使用了指针变量一样。(传递的就是原值),它能够让你直接对原数进行操作,而不是对那个原数的Copy进行操作

对于引用类型:

如果不使用ref /out,因为传递的是引用类型的地址值,则将传递引用类型的地址值的一个COPY(--针对地址值的角度还是值类型传递),实际上就是新开一个不同的内存变量来存储这个地址值的拷贝

而使用ref /out,传递的还是引用类型的地址值,但是传递的就不是一个新变量来存拷贝,而是就是传原来的那个应用类型的地址值

/////////////////注意list的处理////////不用关心,异步,代理委托这些;可以和使用同步方法一样理解;只是正好遇到了这个例////////////////////////////////////////////////子所以拿来用;

public delegate string DelegateWithParameters(string param1, int param2, ArrayList list);

private void CallFooWithParameters()
{
// create the paramets to pass to the function
string strParam1 = "Param1";
int intValue = 100;
ArrayList list = new ArrayList();
list.Add("Item1");

// create the delegate
DelegateWithParameters delFoo =
new DelegateWithParameters(FooWithParameters);

// call FooWithParameters(string param1,int param2, ArrayList list)

// 实际上list的引用地址作为值类型传递,进入方法时,用一个新内存变量存储这个引用的地址

//所以在函数内部多该新内存变量地址修改.只是让该新内存变量指向了另外一个list
IAsyncResult tag =
delFoo.BeginInvoke(strParam1, intValue, list, null, null);

// normally control is returned right away,
// so you can do other work here...

// calling end invoke to get the return value
string strResult = delFoo.EndInvoke(tag);

// write down the parameters:
Trace.WriteLine("param1: " + strParam1);
Trace.WriteLine("param2: " + intValue);
Trace.WriteLine("ArrayList count: " + list.Count);
}

private string FooWithParameters(string param1,
int param2, ArrayList list)
{
// lets modify the data!
param1 = "Modify Value for param1";
param2 = 200;
list = new ArrayList();
//list是传进来的那个地址的拷贝;所以只是把这个新的拷贝指向了一个新的new ArralyList()对象

//所以没有改变原来的值
return "Thank you for reading this article";
}

///////console output

param1: Param1
param2: 100
ArrayList count: 1

没有ref时,用个新的变量地址为4000的存储list的地址

而使用Ref out方式的/////////////////////////

public delegate string DelegateWithOutAndRefParameters(string param1,
out int param2, ref ArrayList list);

private void CallFooWithOutAndRefParameters()
{
// create the paramets to pass to the function
string strParam1 = "Param1";
int intValue = 100;
ArrayList list = new ArrayList();
list.Add("Item1");

// create the delegate
DelegateWithOutAndRefParameters delFoo =
new DelegateWithOutAndRefParameters(FooWithOutAndRefParameters);

// call the beginInvoke function!
IAsyncResult tag =
delFoo.BeginInvoke(strParam1,
out intValue,
ref list,
null, null);

//使用ref,则在进入方法时,不开新的内存变量传的就是原来list的地址的变量

//所以在函数里,可以修改

// normally control is returned right away,
// so you can do other work here...

// calling end invoke notice that intValue and list are passed
// as arguments because they might be updated within the function.
string strResult =
delFoo.EndInvoke(out intValue, ref list, tag);

// write down the parameters:
Trace.WriteLine("param1: " + strParam1);
Trace.WriteLine("param2: " + intValue);
Trace.WriteLine("ArrayList count: " + list.Count);
Trace.WriteLine("return value: " + strResult);
}

private string FooWithOutAndRefParameters(string param1,
out int param2, ref ArrayList list)
{
// lets modify the data!
param1 = "Modify Value for param1";
param2 = 200;
list = new ArrayList();
//
return "Thank you for reading this article";
}

param1: Param1
param2: 200
ArrayList count: 0
return value: Thank you for reading this article

用ref的方法: 还是原来的变量地址1000

------------------------------------------------

由以上可以看到他们的区别;但又会有个问题了,其实我们平时对使用引用类型,在方法中带ref/out 还是不使用ref/out比如list;对它进行操作,其实都是都可以直接改变原来的list内部的值,为什么呢?

因为其实我们平时使用,都是把引用类型传进来,对他进行操作.比如list添加和删除元素.

而但如果对传进来的这个引用地址进行操作改变,则会有区别了

方法中: list = new ArrayList();

对list这个引用地址:

1.不使用ref/out修饰符:实际是一个新的用来存储地址的内存变量(例子中是4000),对它进行重新分配,并不影响原来的list值,因为原来的list引用地址是1000

2.如果使用ref/out修饰符,在方法内,因为生成的引用变量还是1000,所以当给list设置新内容,则会对原来的有影响.

以上示例引用

http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

这是个分析异步操作很多细节的地方

---------------------------------------------------------

而 Ref和Out关键字的区别:

使用Ref型参数时,传入的参数必须先被初始化。而Out则不需要,对Out而言,就必须在方法中对其完成初始化

首先:两者都是按地址传递的,使用后都将改变原来的数值。很多人在论坛上解释说out是按数值传递,是错误的。简单的测试后可以知道out使用也能改变数值的,所以肯定是按照地址传递的。
其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所以你必须初始化一次。这个就是两个的区别,或者说就像有的网友说的,ref是有进有出,out是只出不进。经典!!!

Out更适合用在需要Return多个返回值的地方,而Ref则用在需要被调用的方法修改调用者的引用的时候。

转自:http://blog.csdn.net/ILOVE_NET/archive/2009/03/09/3973835.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: