真实世界的例子,C#“出”参数是有用的? [英] Real-world examples where C# 'out' parameters are useful?

查看:130
本文介绍了真实世界的例子,C#“出”参数是有用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在核心C#编程构造阅读并不好受包装我周围的伸头参数修改。我知道它做什么通过阅读,但我想尝试一之情况时,我会使用它。

I'm reading up on core C# programming constructs and having a hard time wrapping my head around the out parameter modifier. I know what it does by reading but am trying to think of a scenerio when I would use it.

有人可以给我一个真实世界的例子吗?谢谢你。

Can someone give me a real-world example? Thanks.

推荐答案

有很多情况下,您会使用它,但主要是在您的方法需要返回更多然后一个参数。举个例子来说,在 INT 的TryParse 输入方法。在这种情况下,而不是抛出异常的布尔返回为一个成功/失败标志和解析int是回报的出参数。如果你打电话给 int.Parse(...)你可能会抛出异常。

there are many scenarios where you would use it, but the main one would be where your method needs to return more then one parameter. Take, for example, the TryParse methods on int type. In this case, instead of throwing an exception a bool is returned as a success/failure flag and the parsed int is return as the out param. if you were to call int.Parse(...) you could potentially throw an exception.

string str = "123456";
int val;
if ( !int.TryParse(str,out val) )
{
// do some error handling, notify user, etc.
}

这篇关于真实世界的例子,C#“出”参数是有用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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