现在的位置: 首页 > 综合 > 正文

C# 参数关键字out

2012年05月16日 ⁄ 综合 ⁄ 共 373字 ⁄ 字号 评论关闭

out关键字会导致参数通过引用来传递,这与ref关键字类似,不同之处在于 ref
要求变量必须在传递之前进行初始化

当希望方法返回多个值时,声明out方法很有用,示例如下:

int i ;
string
str1, str2;
UseOut(
out
i,out str1,out str2);
// i is now 44
// str1 is now "
字符串1"
// str2 is (still) null;

public
static
void UseOut(out
int i,out
string
str1,out string
str2)
{
i
= 44
;
str1
= "
字符串1";
str2
= null;
}

注意:

1.使用out参数,方法定义和方法使用都必须显示使用out关键字

2.尽管作为 out
参数传递的变量不必在传递之前进行初始化,但需要调用方法以便在方法返回之前赋值。

3.属性不是变量,因此不能作为 out
参数传递

抱歉!评论已关闭.