王朝网络
分享
 
 
 

c#中通过值和引用传递参数(downmoon)

王朝c#·作者佚名  2006-05-27
宽屏版  字体: |||超大  

c#中通过值和引用传递参数(downmoon)

在 C# 中,既可以通过值也可以通过引用传递参数。通过引用传递参数允许函数成员(方法、属性、索引器、运算符和构造函数)更改参数的值,并保持该更改。若要通过引用传递参数,请使用 ref 或 out 关键字。为简单起见,本主题的示例中只使用了 ref 关键字。有关 ref 和 out 之间的差异的信息,请参见、使用 ref 和 out 传递数组

本主题包括下列章节:

传递值类型参数

传递引用类型参数

它还包括以下示例:

示例

演示

是否使用 ref 或 out

1

通过值传递值类型

2

通过引用传递值类型

3

交换值类型(两个整数)

4

通过值传递引用类型

5

通过引用传递引用类型

6

交换引用类型(两个字符串)

传递值类型参数

值类型变量直接包含其数据,这与引用类型变量不同,后者包含对其数据的引用。因此,向方法传递值类型变量意味着向方法传递变量的一个副本。方法内发生的对参数的更改对该变量中存储的原始数据无任何影响。如果希望所调用的方法更改参数值,必须使用 ref 或 out 关键字通过引用传递该参数。为了简单起见,以下示例使用 ref。

示例 1:通过值传递值类型

下面的示例演示通过值传递值类型参数。通过值将变量 myInt 传递给方法 SquareIt。方法内发生的任何更改对变量的原始值无任何影响。

// PassingParams1.cs

using System;

class PassingValByVal

{

static void SquareIt(int x)

// The parameter x is passed by value.

// Changes to x will not affect the original value of myInt.

{

x *= x;

Console.WriteLine("The value inside the method: {0}", x);

}

public static void Main()

{

int myInt = 5;

Console.WriteLine("The value before calling the method: {0}",

myInt);

SquareIt(myInt); // Passing myInt by value.

Console.WriteLine("The value after calling the method: {0}",

myInt);

}

}

输出The value before calling the method: 5

The value inside the method: 25

The value after calling the method: 5

代码讨论

变量 myInt 为值类型,包含其数据(值 5)。当调用 SquareIt 时,myInt 的内容被复制到参数 x 中,在方法内将该参数求平方。但在 Main 中,myInt 的值在调用 SquareIt 方法之前和之后是相同的。实际上,方法内发生的更改只影响局部变量 x。

示例 2:通过引用传递值类型

下面的示例除使用 ref 关键字传递参数以外,其余与“示例 1”相同。参数的值在调用方法后发生更改。

// PassingParams2.cs

using System;

class PassingValByRef

{

static void SquareIt(ref int x)

// The parameter x is passed by reference.

// Changes to x will affect the original value of myInt.

{

x *= x;

Console.WriteLine("The value inside the method: {0}", x);

}

public static void Main()

{

int myInt = 5;

Console.WriteLine("The value before calling the method: {0}",

myInt);

SquareIt(ref myInt); // Passing myInt by reference.

Console.WriteLine("The value after calling the method: {0}",

myInt);

}

}

输出The value before calling the method: 5

The value inside the method: 25

The value after calling the method: 25

代码讨论

本示例中,传递的不是 myInt 的值,而是对 myInt 的引用。参数 x 不是 int 类型,它是对 int 的引用(本例中为对 myInt 的引用)。因此,当在方法内对 x 求平方时,实际被求平方的是 x 所引用的项:myInt。

示例 3:交换值类型

更改所传递参数的值的常见示例是 Swap 方法,在该方法中传递 x 和 y 两个变量,然后使方法交换它们的内容。必须通过引用向 Swap 方法传递参数;否则,方法内所处理的将是参数的本地副本。以下是使用引用参数的 Swap 方法的示例:

static void SwapByRef(ref int x, ref int y)

{

int temp = x;

x = y;

y = temp;

}

调用该方法时,请在调用中使用 ref 关键字,如下所示:

SwapByRef (ref i, ref j);

传递引用类型参数

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

示例 4:通过值传递引用类型

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

// PassingParams4.cs

// Passing an array to a method without the ref keyword.

// Compare the results to those of Example 5.

using System;

class PassingRefByVal

{

static void Change(int[] arr)

{

arr[0]=888; // This change affects the original element.

arr = new int[5] {-3, -1, -2, -3, -4}; // This change is local.

Console.WriteLine("Inside the method, the first element is: {0}", arr[0]);

}

public static void Main()

{

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

Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", myArray [0]);

Change(myArray);

Console.WriteLine("Inside Main, after calling the method, the first element is: {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”相同。方法内发生的任何更改都会影响调用程序中的原始变量。

// PassingParams5.cs

// Passing an array to a method with the ref keyword.

// Compare the results to those of Example 4.

using System;

class PassingRefByRef

{

static void Change(ref int[] arr)

{

// Both of the following changes will affect the original variables:

arr[0]=888;

arr = new int[5] {-3, -1, -2, -3, -4};

Console.WriteLine("Inside the method, the first element is: {0}", arr[0]);

}

public static void Main()

{

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

Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", myArray [0]);

Change(ref myArray);

Console.WriteLine("Inside Main, after calling the method, the first element is: {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 内均进行交换。

// PassingParams6.cs

using System;

class SwappinStrings

{

static void SwapStrings(ref string s1, ref string s2)

// The string parameter x is passed by reference.

// Any changes on parameters will affect the original variables.

{

string temp = s1;

s1 = s2;

s2 = temp;

Console.WriteLine("Inside the method: {0}, {1}", s1, s2);

}

public static void Main()

{

string str1 = "John";

string str2 = "Smith";

Console.WriteLine("Inside Main, before swapping: {0} {1}",

str1, str2);

SwapStrings(ref str1, ref str2); // Passing strings by reference

Console.WriteLine("Inside Main, after swapping: {0}, {1}",

str1, str2);

}

}

输出Inside Main, before swapping: John Smith

Inside the method: Smith, John

Inside Main, after swapping: Smith, John

代码讨论

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有