首页 技术 正文
技术 2022年11月12日
0 收藏 803 点赞 4,445 浏览 1948 个字
  • 引用类型的变量不直接包含其数据;它包含的是对其数据的引用。 当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值。 但是无法更改引用本身的值;也就是说,不能使用相同的引用为新类分配内存并使之在块外保持。 若要这样做,应使用 ref 或 out 关键字传递参数。
  • 通过ref允许调用方法来修改引用参数引用的对象。 对象的存储位置传递给方法作为引用参数的值。 如果您修改了更改参数的存储位置,那么您也更改了基础参数的存储位置。

new 运算符来分配新的内存部分,将使变量 pArray 引用新的数组。 因此,这之后的任何更改都不会影响原始数组 arr(它是在 Main 内创建的)。 实际上,本示例中创建了两个数组,一个在 Main 内,一个在Change 方法内。

通过引用传递引用类型

下面的示例与前面的示例,除此之外, ref 关键字添加到方法标头并调用。 在方法影响出现在调用过程的原始变量的任何更改。

C#

class PassingRefByRef
{
static void Change(ref int[] pArray)
{
// Both of the following changes will affect the original variables:
pArray[0] = 888;
pArray = new int[5] {-3, -1, -2, -3, -4};
System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
} static void Main()
{
int[] arr = {1, 4, 5};
System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]); Change(ref arr);
System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);
}
}
/* 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: -3
*/

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

交换两个字符串

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

C#

 class SwappingStrings
{
static void SwapStrings(ref string s1, ref string s2)
// The string parameter is passed by reference.
// Any changes on parameters will affect the original variables.
{
string temp = s1;
s1 = s2;
s2 = temp;
System.Console.WriteLine("Inside the method: {0} {1}", s1, s2);
} static void Main()
{
string str1 = "John";
string str2 = "Smith";
System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2); SwapStrings(ref str1, ref str2); // Passing strings by reference
System.Console.WriteLine("Inside Main, after swapping: {0} {1}", str1, str2);
}
}
/* Output:
Inside Main, before swapping: John Smith
Inside the method: Smith John
Inside Main, after swapping: Smith John
*/

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295