在C#中ref 和 out有什么区别?应注意哪些地方?

XPR 2005-10-16 11:28:27

在C#中ref 和 out有什么区别?应注意哪些地方?
谢谢!
...全文
1042 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
fcyz 2005-11-18
  • 打赏
  • 举报
回复
谢谢大家
LGame 2005-10-17
  • 打赏
  • 举报
回复
学习
shilei831115 2005-10-17
  • 打赏
  • 举报
回复
能不能给出具体的事例?
jijl2001 2005-10-17
  • 打赏
  • 举报
回复
ref 必须在先赋值
out 无须先赋值. 但在方法内必须给它赋值.
hzw66 2005-10-17
  • 打赏
  • 举报
回复
都是以引用传递参数的
ref可以同时做为输入、输出参数
out只做为输出参数,所以可以不初始化
siugwan 2005-10-17
  • 打赏
  • 举报
回复
mark
SqlDataAdapter 2005-10-17
  • 打赏
  • 举报
回复
如果作为参数的话,ref要调用之前分配内存,out在函数内部分配内存。
xiaomatian 2005-10-17
  • 打赏
  • 举报
回复
前一个传过去之前参数必须初始化,后一个不必要
XPR 2005-10-17
  • 打赏
  • 举报
回复
感谢大家的帮助!
谢谢!
xiaomatian 2005-10-17
  • 打赏
  • 举报
回复
当然你也可以在main里不给x1,y1赋值!
xiaomatian 2005-10-17
  • 打赏
  • 举报
回复
给你个代码看看吧。刚刚写的。希望你看了能有所了解。在控制台下看看输出结果你就知道了。
using System;
using System.Collections;

public class MyClass
{
public static void Main()
{
//code goes here;
int i=1,j=2;
int a=1,s=2;
int x1=1,y1=2;
Console.WriteLine("Before use Out!");
Console.WriteLine("x1={0},y1={1}",x1,y1);
Swap(ref i,ref j);
Swap1(a,s);
Swap2(out x1,out y1);
Console.WriteLine("after use out!");
Console.WriteLine("x1={0},y1={1}",x1,y1);
Console.WriteLine("a={0},s={1}",a,s);
Console.WriteLine("after use ref!");
Console.WriteLine("i={0},j={1}",i,j);
RL();
}
//static void Swap1(int a,int s)
static void Swap1(int a , int s)
{
int temp=a;
a=s;
s=temp;
}
static void Swap(ref int x,ref int y)
{
int temp=x;
x=y;
y=temp;
}
static void Swap2(out int x1,out int y1)
{
x1=3;
y1=4;
}
}
claus2001 2005-10-17
  • 打赏
  • 举报
回复
mark
zhy0101 2005-10-17
  • 打赏
  • 举报
回复
方法参数上的 out 方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。

当希望方法返回多个值时,声明 out 方法非常有用。使用 out 参数的方法仍然可以返回一个值。一个方法可以有一个以上的 out 参数。

若要使用 out 参数,必须将参数作为 out 参数显式传递到方法。out 参数的值不会传递到 out 参数。

不必初始化作为 out 参数传递的变量。然而,必须在方法返回之前为 out 参数赋值。

属性不是变量,不能作为 out 参数传递。

如果两个方法的声明仅在 out 的使用方面不同,则会发生重载。不过,无法定义仅在 ref 和 out 方面不同的重载。例如,以下重载声明是有效的:

class MyClass
{
public void MyMethod(int i) {i = 10;}
public void MyMethod(out int i) {i = 10;}
}
而以下重载声明是无效的:

class MyClass
{
public void MyMethod(out int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
有关传递数组的信息,请参见使用 ref 和 out 传递数组。

示例
// cs_out.cs
using System;
public class MyClass
{
public static int TestOut(out char i)
{
i = 'b';
return -1;
}

public static void Main()
{
char i; // variable need not be initialized
Console.WriteLine(TestOut(out i));
Console.WriteLine(i);
}
}
输出
-1
b
[摘自MSDN]
zhy0101 2005-10-17
  • 打赏
  • 举报
回复
方法参数上的 ref 方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。

若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。ref 参数的值被传递到 ref 参数。

传递到 ref 参数的参数必须最先初始化。将此方法与 out 参数相比,后者的参数在传递到 out 参数之前不必显式初始化。

属性不是变量,不能作为 ref 参数传递。

如果两种方法的声明仅在它们对 ref 的使用方面不同,则将出现重载。但是,无法定义仅在 ref 和 out 方面不同的重载。例如,以下重载声明是有效的:

class MyClass
{
public void MyMethod(int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
但以下重载声明是无效的:

class MyClass
{
public void MyMethod(out int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
有关传递数组的信息,请参见使用 ref 和 out 传递数组。

示例
// cs_ref.cs
using System;
public class MyClass
{
public static void TestRef(ref char i)
{
// The value of i will be changed in the calling method
i = 'b';
}

public static void TestNoRef(char i)
{
// The value of i will be unchanged in the calling method
i = 'c';
}

// This method passes a variable as a ref parameter; the value of the
// variable is changed after control passes back to this method.
// The same variable is passed as a value parameter; the value of the
// variable is unchanged after control is passed back to this method.
public static void Main()
{

char i = 'a'; // variable must be initialized
TestRef(ref i); // the arg must be passed as ref
Console.WriteLine(i);
TestNoRef(i);
Console.WriteLine(i);
}
}
输出
b
b
[摘自MSDN]
lovebanyi 2005-10-16
  • 打赏
  • 举报
回复
ref 必须在先赋值
out 无须先赋值. 但在方法内必须给它赋值.
xhan2000 2005-10-16
  • 打赏
  • 举报
回复
iny y=x;错误
xhan2000 2005-10-16
  • 打赏
  • 举报
回复
ref int x
int y=x;
x=123;
都可以
out int x
iny y=123;错误
x=123正确
whmjw 2005-10-16
  • 打赏
  • 举报
回复
前一个传过去之前参数必须初始化,后一个不必要

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧