REF之间,并在.NET中的参数差异 [英] Difference between ref and out parameters in .NET

查看:138
本文介绍了REF之间,并在.NET中的参数差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET之间的区别REF 退出参数是什么?什么是其中一个可以比另一个更有益的情况呢?什么将是一个code段,其中一个可以用来和其他不能?

What is the difference between ref and out parameters in .NET? What are the situations where one can be more useful than the other? What would be a code snippet where one can be used and another can't?

推荐答案

他们是pretty的大致相同 - 唯一的区别是,你传递一个变量的退出参数不需要进行初始化,并使用 REF 参数的方法,将其设置为东西。

They're pretty much the same - the only difference is that a variable you pass as an out parameter doesn't need to be initialised, and the method using the ref parameter has to set it to something.

int x;
Foo(out x); // OK

int y;
Foo(ref y); // Error

参考参数是可能被修改的数据,退出参数数据这是一个额外的输出为功能(如 int.TryParse )已经在使用的返回值的东西。

Ref parameters are for data that might be modified, out parameters are for data that's an additional output for the function (eg int.TryParse) that are already using the return value for something.

这篇关于REF之间,并在.NET中的参数差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆