ref和out关键字的用法
来源:互联网 发布:淘宝卖家资质50000 编辑:程序博客网 时间:2024/05/27 16:42
ref和out关键字的用法
ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。例如:
class RefExample
{
static void Method(ref int i)
{
i = 44;
}
static void Main()
{
int val = 0;
Method(ref val);
// val is now 44
}
}
传递到 ref 参数的参数必须最先初始化。这与 out 不同,out 的参数在传递之前不需要显式初始化,例如:
class OutExample
{
static void Method(out int i)
{
i = 44;
}
static void Main()
{
int value;
Method(out value);
// value is now 44
}
}
ref 和 out 关键字在运行时的处理方式不同,但在编译时的处理方式相同。因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译以下代码:
class CS0663_Example
{
// compiler error CS0663: "cannot define overloaded
// methods that differ only on ref and out"
public void SampleMethod(out int i) { }
public void SampleMethod(ref int i) { }
}
但是,如果一个方法采用 ref 或 out 参数,而另一个方法不采用这两类参数,则可以进行重载,如下所示:
class RefOutOverloadExample
{
public void SampleMethod(int i) { }
public void SampleMethod(out int i) { }
}
当希望方法返回多个值时,声明 out 方法很有用。使用 out 参数的方法仍然可以将变量用作返回类型,但它还可以将一个或多个对象作为 out 参数返回给调用方法。此示例使用 out 在一个方法调用中返回三个变量。请注意,第三个参数所赋的值为 Null。这样便允许方法有选择地返回值。
class OutReturnExample
{
static void Method(out int i, out string s1, out string s2)
{
i = 44;
s1 = "I've been returned";
s2 = null;
}
static void Main()
{
int value;
string str1, str2;
Method(out value, out str1, out str2);
// value is now 44
// str1 is now "I've been returned"
// str2 is (still) null;
}
}
二:使用 ref 和 out 传递数组
示例1
class TestOut
{
static void FillArray(out int[] arr)
{
// Initialize the array:
arr = new int[5] { 1, 2, 3, 4, 5 };
}
static void Main()
{
int[] theArray; // Initialization is not required
// Pass the array to the callee using out:
FillArray(out theArray);
// Display the array elements:
System.Console.WriteLine("Array elements are:");
for (int i = 0; i < theArray.Length; i++)
{
System.Console.Write(theArray[i] + " ");
}
}
}
输出:
Array elements are:
1 2 3 4 5
在上例中,在调用方(Main 方法)中声明数组 theArray,并在 FillArray 方法中初始化此数组。然后将数组元素返回调用方并显示.
示例2:
在上例中,在调用方(Main 方法)中初始化数组 theArray,并通过使用 ref 参数将其传递给 FillArray 方法。在 FillArray 方法中更新某些数组元素。然后将数组元素返回调用方并显示。
class TestRef
{
static void FillArray(ref int[] arr)
{
// Create the array on demand:
if (arr == null)
{
arr = new int[10];
}
// Fill the array:
arr[0] = 1111;
arr[4] = 5555;
}
static void Main()
{
// Initialize the array:
int[] theArray = { 1, 2, 3, 4, 5 };
// Pass the array using ref:
FillArray(ref theArray);
// Display the updated array:
System.Console.WriteLine("Array elements are:");
for (int i = 0; i < theArray.Length; i++)
{
System.Console.Write(theArray[i] + " ");
}
}
}
输出:
Array elements are:
1111 2 3 4 5555
- ref和out关键字的用法
- ref 和 out关键字的用法
- C# ref 和 out ,params关键字的用法
- C# out、ref关键字的用法和区别
- C#关键字params, ref, out的用法
- ref和out的用法
- ref和out的用法
- 关键字 ref和out
- 关键字ref和out
- 对于关键字Ref和Out的理解
- 对于关键字Ref和Out的理解
- C#的ref和out关键字【转载】
- C#关键字ref和out的区别
- ref和out关键字的区别
- 关键字 ref 和 out 的区别
- ref 和 out 用法
- ref和out的用法和区别
- 关于ref 和out 的用法---困惑
- C# 格式化字符串几种方式总结 String.Format
- 内核是如何管理内存的?
- C#建立文件夹
- 运行时类型识别RTTI
- android找不到avd如何解决?
- ref和out关键字的用法
- bmp格式
- Webkit和Trident内核的黑白网页测试
- ssh的防御办法
- Content is not allowed in prolog
- 如何制作安装程序
- ASP.NET中的缓存使用(1)
- 单例模式的问题
- 现在的歌名越来越怪了