可选输出参数? [英] Optional out parameters?

查看:77
本文介绍了可选输出参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个建议。


当一个方法有一个out参数时,调用代码必须提供一个

目标变量。我希望能够传递null,

表示我没有使用out参数。


例如,如果我有一个方法...

bool IsValidWeight(浮动重量,输出字符串错误)

....我可能想检查一个重量是否有效而不关心

关于特定错误(值为负,值太高,

等),在这种情况下声明并传递未使用的变量似乎

不必要地使代码复杂化。


你怎么看?


P.

This is just a suggestion.

When a method has an out parameter, the calling code has to supply a
target variable. I would like to be able to pass null instead,
indicating that I have no use for the out parameter.

For example, if I have a method ...
bool IsValidWeight(float weight, out string error)
.... I might want to check whether a weight is valid without caring
about the specific error ("value is negative", "value is too high",
etc.), in which case declaring and passing the unused variable seems
to complicate the code unnecessarily.

What do you think?

P.

推荐答案

Paul,


我不太确定我喜欢它,只因为它看起来很奇怪

签名是一个值类型,如下所示:


bool InvalidWeight(浮动权重,输出int错误);


然后使用null是非-intuitive,会损害

语言,IMO。

-

- Nichol作为Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Paul E Collins" <音响****************** @ CL4.org>在消息中写道

news:ct ********** @ hercules.btinternet.com ...
Paul,

I am not too sure I like it, only because it looks odd when the
signature is a value type, like this:

bool InvalidWeight(float weight, out int error);

Then the use of null is non-intuitive, and would be a detriment to the
language, IMO.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:ct**********@hercules.btinternet.com...
这只是一个建议。

当一个方法有一个out参数时,调用代码必须提供一个
目标变量。我希望能够传递null,表示我没有使用out参数。

例如,如果我有方法...
bool IsValidWeight(浮动重量,输出字符串错误)
...我可能想检查一个重量是否有效而不关心特定错误(值为负,值太高 (等等),在哪种情况下声明并传递未使用的变量似乎不必要地使代码复杂化。

你怎么看?
P.
This is just a suggestion.

When a method has an out parameter, the calling code has to supply a
target variable. I would like to be able to pass null instead, indicating
that I have no use for the out parameter.

For example, if I have a method ...
bool IsValidWeight(float weight, out string error)
... I might want to check whether a weight is valid without caring about
the specific error ("value is negative", "value is too high", etc.), in
which case declaring and passing the unused variable seems to complicate
the code unnecessarily.

What do you think?

P.



在C#中,您可以通过提供备用方法签名来实现此目的,

而不会有违规行为参数。


替代实现可以只声明out参数,

将其传递给完整方法实现,并且根本不返回

参数。

In C# you achieve this by providing an alternate method signature,
without the offending out argument.

The alternate implementation can then just declare the out argument,
pass it to the full method implementation, and simply not return that
argument.


Paul E Collins< fi ****************** @ CL4.org>写道:
Paul E Collins <fi******************@CL4.org> wrote:
这只是一个建议。

当一个方法有一个out参数时,调用代码必须提供一个
目标变量。我希望能够传递null,
表示我没有使用out参数。

例如,如果我有方法...
bool IsValidWeight(浮动重量,输出字符串错误)
...我可能想要检查重量是否有效而不关心特定错误(值为负,值太高 ;,
等),在这种情况下,声明并传递未使用的变量似乎不必要地使代码复杂化。

你怎么看?
This is just a suggestion.

When a method has an out parameter, the calling code has to supply a
target variable. I would like to be able to pass null instead,
indicating that I have no use for the out parameter.

For example, if I have a method ...
bool IsValidWeight(float weight, out string error)
... I might want to check whether a weight is valid without caring
about the specific error ("value is negative", "value is too high",
etc.), in which case declaring and passing the unused variable seems
to complicate the code unnecessarily.

What do you think?



可能最简单的方法是重载方法,所以

你还提供:


bool IsValidWeight(float重量)

{

字符串错误;

返回IsValidWeight(重量,输出错误);

}


(请注意,异常通常是指示

错误的更好方式。)


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复对于小组,请不要给我发邮件



Probably the easiest way of doing this is overloading the method, so
you also provide:

bool IsValidWeight (float weight)
{
string error;
return IsValidWeight (weight, out error);
}

(Note that exceptions would usually be a better way of indicating
errors.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于可选输出参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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