What is diffrence between Out type Vs Ref type parameter in C#?
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Difference between ref and out parameters in .NET
C.NET中的out类型和ref类型参数有什么不同?
什么时候我们可以在哪种情况下使用?
从这里引用
The out keyword causes arguments to be passed by reference. This is
similar to the ref keyword, except that ref requires that the variable
be initialized before being passed.
有些方法,如
两者都向调用方指示该方法可以修改参数的值。
1 2 3 | Foo foo; SomeMethod(out foo); // at this stage we know that foo will be initialized |
鉴于参考号:
1 2 | Foo foo; SomeMethod(ref foo); // compile time error |
调用方负责在调用方法之前初始化变量:
1 2 |