ref和out都是C#中的关键字,所实现的功能也差不多,都是指定一个参数按照引用传递。
对于编译后的程序而言,它们之间没有任何区别,也就是说它们只有语法区别。
对于编译后的程序而言,它们之间没有任何区别,也就是说它们只有语法区别。
总结起来,他们有如下语法区别:
1、ref传进去的参数必须在调用前初始化,out不必,即:
<pre name="code" class="csharp">int i;
<pre>SomeMethod( ref i );//语法错误
SomeMethod( out i );//通过
2、ref传进去的参数在函数内部可以直接使用,而out不可:
public void SomeMethod(ref int i)
{
int j=i;//通过
//...
}
public void SomeMethod(out int i)
{
int j=i;//语法错误
}<strong>
</strong>