C#的引用参数和值参数
来源:互联网 发布:常见网络故障诊断工具 编辑:程序博客网 时间:2024/04/28 23:07
ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
2.1. 若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。
2.2. 传递到 ref 参数的参数必须最先初始化。这与 out 不同,out 的参数在传递之前不需要显式初始化。
2.3. 如果一个方法采用 ref 或 out 参数,而另一个方法不采用这两类参数,则可以进行重载。
示例 1:通过值传递值类型
下面的示例演示通过值传递值类型参数。通过值将变量 myInt 传递给方法 SquareIt。方法内发生的任何更改对变量的原始值无任何影响。
class PassingValByVal{ static void SquareIt(int x) { x *= x; Console.WriteLine("The valueinside the method: {0}", x); } public static void Main() { int myInt = 5; Console.WriteLine("The valuebefore calling the method: {0}", myInt); SquareIt(myInt); // Passing myInt byvalue. Console.WriteLine("The valueafter calling the method: {0}", myInt); Console.ReadKey(); }}
输出
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”相同。参数的值在调用方法后发生更改。
class PassingValByRef{ static void SquareIt(ref int x) { x *= x; Console.WriteLine("The valueinside the method: {0}", x); } public static void Main() { int myInt = 5; Console.WriteLine("The valuebefore calling the method: {0}", myInt); SquareIt(ref myInt); Console.WriteLine("The valueafter calling the method: {0}", myInt); Console.ReadKey(); }}
输出
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。
class PassingRefByVal{ static void Change(int[] arr) { arr[0] = 888; arr = new int[5] { -3, -1, -2, -3,-4 }; Console.WriteLine("Inside themethod, 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]);Console.ReadKey(); }}
输出
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”相同。方法内发生的任何更改都会影响调用程序中的原始变量。
class PassingRefByRef{ static void Change(ref int[] arr) { // Both of the following changeswill affect the original variables: arr[0] = 888; arr = new int[5] { -3, -1, -2, -3,-4 }; Console.WriteLine("Inside themethod, 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]);Console.ReadKey(); }}
输出
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方法中创建的五个元素的数组。
3. 输出类型(out类型)
out 关键字会导致参数通过引用来传递。这与 ref关键字类似。
与 ref 的不同之处:
3.1. ref 要求变量必须在传递之前进行初始化,out 参数传递的变量不需要在传递之前进行初始化。
3.2. 尽管作为 out 参数传递的变量不需要在传递之前进行初始化,但需要在调用方法初始化以便在方法返回之前赋值。
- C#的引用参数和值参数
- C#的引用参数和值参数
- C#的值参数 输出参数 引用参数
- C#引用参数和输出参数的区别
- C#引用参数和输出参数的区别
- C#学习笔记二 引用参数和值参数
- 值参数,输出参数和引用参数
- C#参量参数、输出参数、引用参数、值参数
- C#参数的传递:值传递、引用传递、输出参数
- C#形参,实参,值传递参数,引用传递参数,输出参数,参数数组的学习
- C#的引用(ref)参数
- 值和引用参数
- C#中值参数、引用参数、输出参数的区别
- C#基础学习--方法参数的种类,值参数,引用参数(ref),输出参数(out),数组参数(params)
- 引用参数和传值参数的区别
- 引用参数和传值参数的区别深入解析
- C#中的值参数,引用参数及输出参数
- C#_delegate - 值参数和引用参数
- LINQ 查询语法
- JavaWeb -- Struts1 多文件上传与下载 DownloadAction, DispatchAction
- C# 委托知识总结
- 凭证和单据
- C# 参数数组
- C#的引用参数和值参数
- PDF-XChange 注册码
- CS0234: 命名空间“PetShop”中不存在类型或命名空间名称“Model“
- 为什么不能通过new List()方式创建对象
- Linux 下 FreeSWITCH 远程连接使用 MySQL 替代 SQLite
- Linux静态库与动态库的创建
- Oracle 11g解锁scott
- 重新安装MSDTC
- Microsoft Visual Studio 不同的版本,创建对象会报错