1. 区别:
out 会导致参数按照饮用来传递并且不用初始化。
ref 标注参数按照引用方式初始化,并且(definite assignment )明确的赋初值。
class OutExample { static void Method(out int i) { i = 44; } static void Main() { int value; Method(out value); // value is now 44 } }
2 可否作为重载标志?
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) { } }
本文详细解析了C#编程语言中ref与out关键字的使用差异,包括它们如何改变参数传递方式及其对方法重载的影响。通过具体示例说明了两者在初始化和赋值方面的不同,并解释了为何不能仅凭ref与out来实现方法重载。
773

被折叠的 条评论
为什么被折叠?



