中通过值和引用传递参数覆盖

金融专用设备2021年09月06日

传递引用类型参数

引用类型的变量不直接包含其数据;它包含的是对其数据的引用。当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值。但是无法更改引用本身的值;也就是说,不能使用相同的引用为新类分配内存并使之在块外保持。若要这样做,请使用 ref(或 out)关键字传递参数。为了简单起见,以下示例使用 ref。

示例 4:通过值传递引用类型下面的示例演示通过值向 Change 方法传递引用类型的参数 myArray。由于该参数是对 myArray 的引用,所以有可能更改数组元素的值。但是,试图将参数重新分配到不同的内存位置时,该操作仅在方法内有效,并不影响原始变量 myArray。

//

//Passinganarraytoamethodwithouttherefkeyword.

//ComparetheresultstothoseofExample5.

usingSystem;

classPassingRefByVal

...{

staticvoidChange(int[]arr)

...{

arr[0]=888;//Thischangeaffectstheoriginalelement.

arr=newint[5]...{-3,-1,-2,-3,-4};//Thischangeislocal.

iteLine(\"Insidethemethod,thefirstelementis:{0}\",arr[0]);

}

publicstaticvoidMain()

...{

int[]myArray=...{1,4,5};

iteLine(\"InsideMain,beforecallingthemethod,thefirstelementis:{0}\",myArray[0]);

Change(myArray);

iteLine(\"InsideMain,aftercallingthemethod,thefirstelementis:{0}\",myArray[0]);

}

}

输出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

代码讨论在上个示例中,数组 myArray 为引用类型,在未使用 ref 参数的情况下传递给方法。在此情况下,将向方法传递指向 myArray 的引用的一个副本。输出显示方法有可能更改数组元素的内容(从 1 改为 888)。但是,在 Change 方法内使用 new 运算符分配新的内存部分,将使变量 arr 引用新的数组。因此,这之后的任何更改都不会影响原始数组 myArray(它是在 Main 内创建的)。实际上,本示例中创建了两个数组,一个在 Main 内,一个在 Change 方法内。

示例 5:通过引用传递引用类型本示例除在方法头和调用中使用 ref 关键字以外,其余与“示例 4”相同。方法内发生的任何更改都会影响调用程序中的原始变量。

//

//Passinganarraytoamethodwiththerefkeyword.

//ComparetheresultstothoseofExample4.

usingSystem;

classPassingRefByRef

...{

staticvoidChange(refint[]arr)

...{

//Bothofthefollowingchangeswillaffecttheoriginalvariables:

arr[0]=888;

arr=newint[5]...{-3,-1,-2,-3,-4};

iteLine(\"Insidethemethod,thefirstelementis:{0}\",arr[0]);

}

publicstaticvoidMain()

...{

int[]myArray=...{1,4,5};

iteLine(\"InsideMain,beforecallingthemethod,thefirstelementis:{0}\",myArray[0]);

Change(refmyArray);

iteLine(\"InsideMain,aftercallingthemethod,thefirstelementis:{0}\",myArray[0]);

转变社会发展观。它不断催生新的生产方式 }

}

输出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: -3

代码讨论方法内发生的所有更改都影响 Main 中的原始数组。实际上,使用 new 运算符对原始数组进行了重新分配。因此,调用 Change 方法后,对 myArray 的任何引用都将指向 Change 方法中创建的五个元素的数组。

示例 6:交换两个字符串交换字符串是通过引用传递引用类型参数的很好的示例。本示例中,str1 和 str2 两个字符串在 Main 中初始化,并作为由 ref 关键字修饰的参数传递给 SwapStrings 方法。这两个字符串在该方法内以及 Main 内均进行交换。

//

usingSystem;

classSwappinStrings

...{

staticvoidSwapStrings(refstrings1,refstrings2)

//Thestringparameterxispassedbyreference.

//Anychangesonparameterswillaffecttheoriginalvariables.

...{

stringtemp=s1;

s1=s2;

s2=temp;

iteLine(\"Insidethemethod:{0},{1}\",s1,s2);

}

publicstaticvoidMain()

...{

stringstr1=\"John\";

stringstr2=\"Smith\";

iteLine(\"InsideMain,beforeswapping:{0}{1}\",

str1,str2);

SwapStrings(refstr1,refstr2);//Passingstringsbyreference

iteLine(\"InsideMain,afterswapping:{0},{1}\",

str1,str2);

}

}

输出Inside Main, before swapping: John Smith

Inside the method: Smith, John

Inside Main, after swapping: Smith, John

代码讨论本示例中,需要通过引用传递参数以影响调用程序中的变量。如果同时从方法头和方法调用中移除 ref 关键字,则调用程序中不会发生任何更改。

查看本文来源

北京阴道炎哪家好
呼和浩特白癜风治疗哪家好
地奥氨贝治疗效果好吗
相关阅读
当代风雅大师——石一

石一,1953年7月底生,出身菁英。少年时喜好山水画,在校经常舞文弄墨。未满后...

2023-10-18
杨浦区通报1例全球化面阳性感染者相关情况丨疫情防控新闻发布会

现在(6月15日)下午参加的市非典应对工作新闻发布会上,浦东新区副区长王...

2023-10-01
许凯吸烟照太多因网友吐槽:“网红才爱照这种类型的照片”

说起2018最火的新人,估计值一定会有许凯的一席之地,凭借古装剧《延禧宫...

2023-09-27
香港附近海域运油船着火,6名伤者均为外籍!系从高雄出发

4年末16日16时许,一艘运油驳船在港台东面大共约300公里海域牵涉到引爆,目...

2023-09-23
【东方焦点】北京中医药大学东方医院与海外中医中心共同承办国际代表大会“中医儿科”线上分论坛取得圆满成

北京里药学药大学圣城疗养院与海外里药学区域内联合负责负责管理国际则会...

2023-09-20
@参保人,这五个医保小知识慢记下,看病就医省钱又省心!

如何使用医疗卫生 看病能更“省会一些”呢? 举例来说是去医院看病 为什...

2023-09-19
友情链接