Ref和Out之间的区别是什么?(What is diff Between Ref And Out? [duplicate])

系统教程 行业动态 更新时间:2024-06-14 17:03:54
Ref和Out之间的区别是什么?(What is diff Between Ref And Out? [duplicate])

可能重复: 'ref'和'out'关键字有什么区别?

ref和out什么区别? 我很困惑什么时候使用ref和out 。 所以请解释如何使用ref和out ,以及在哪些情况下。

Possible Duplicate: Whats the difference between the 'ref' and 'out' keywords?

What is the difference between ref and out? I am confused about when to use ref and out. So please explain how to use ref and out, and in which situations.

最满意答案

您在传递初始化参数时使用Ref ,并且您期望方法/函数对其进行修改。

您在传递未初始化的参数时使用Out ,并且该方法必须初始化并填充该参数(否则会发出警告或者甚至出错)。

bool IsUserValid(string username);

无效IsUserValid(字符串的用户名,出bool有效);

上述声明大致相同。 返回值比较容易,所以在这种情况下,您将使用返回类型。 但是如果你的方法还需要返回用户的出生日期,你不能在返回中返回两个参数,你必须使用out参数来返回其中的一个参数(或者使方法无效并返回为out)。

You use Ref when you pass an initialized parameter and you expect the method/function to modify it.

You use Out when you pass an un-initialized parameter and the method will have to initialize and fill that parameter (you get a warning or even error otherwise).

bool IsUserValid(string username);

void IsUserValid(string username, out bool valid);

The declarations above are roughly the same. It's easier to return the value, so in this case you will use the return type. But if your method also needs to return the birth date of the user you can't return both parameters in the return, you have to use out parameters to return one of them (or void the method and return both as out).

更多推荐

本文发布于:2023-04-24 14:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a3de72b6b47ccc28a0bc4268b61266c5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   Ref   duplicate   diff

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!