将2个参数发送回Main函数 [英] Sending 2 parameters back to Main function

查看:69
本文介绍了将2个参数发送回Main函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个函数想要将2个 INT 参数发送回调用位置,但不确定什么是最好的选择,一个字典?或 List Array

I got a function which wants to send 2 INT parameters back to where it has been called, but not sure what would be the best option, a Dictionary ? or a List or an Array

编辑

我正在使用.Net Framework 2

推荐答案

我假设您的意思是向调用方返回2个值。如果是这样的话:

I assume you mean return 2 values to the caller. If so:

  • return a Tuple<T1, T2>
  • return your own custom type which stores the desired values, like MyOperationResult
  • use out parameters: http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.80).aspx
  • return a collection

这取决于呼叫者最容易理解的内容,即,什么最能表明您的意图?

It depends on what is easiest for the caller to understand, i.e. what best demonstrates your intention?

如果结果将始终具有两个值,则返回可变大小的集合可能并不直观。我的偏好是元组或自定义类型。元组的优点是可以作为现有的类型,但缺点是不清楚其成员的含义。它们只是被称为 Item1, Item2,依此类推。

Returning a variable-size collection may not be intuitive if the result will always have two values. My preference is either a Tuple or a custom type. A Tuple has the advantage of being an existing type, but the disadvantage that it's not clear what its members mean; they are just called "Item1", "Item2", and so on.

out 关键字非常适合可能无法成功执行并且总是返回布尔值的操作,例如 public bool int.TryParse(string value,out int parsedValue)。意图很明显。

The out keyword is nice for operations which may not succeed and always return a bool, like public bool int.TryParse( string value, out int parsedValue ). Here the intent is clear.

这篇关于将2个参数发送回Main函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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