c# 关键字:ref 和 out

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

.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";

}

相关文档
最新文档