是否有任何借口从隐式转换中引发Exception? [英] Is there ever an excuse for throwing an Exception from an implicit conversion?

查看:74
本文介绍了是否有任何借口从隐式转换中引发Exception?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN


通过消除不必要的强制转换,隐式转换可以提高源代码的可读性。但是,由于隐式转换可能会在没有程序员指定的情况下发生,因此必须注意防止令人不快的意外。通常,隐式转换运算符不应引发异常,也不会丢失信息,以便可以在程序员不知情的情况下安全地使用它们。如果转换运算符不能满足这些条件,则应将其标记为显式。

By eliminating unnecessary casts, implicit conversions can improve source code readability. However, because implicit conversions can occur without the programmer's specifying them, care must be taken to prevent unpleasant surprises. In general, implicit conversion operators should never throw exceptions and never lose information so that they can be used safely without the programmer's awareness. If a conversion operator cannot meet those criteria, it should be marked explicit.

虽然我不同意任何观点,并且我同意这一切都很好,是否有足够的理由足以使我打破关于隐式转换的部分而不抛出异常?

While I don't disagree with any particular point, and I agree that this is all very good, is there ever a reason that is great enough to warrant breaking the part about implicit conversions not throwing exceptions?

我遇到的特殊情况在我之前是这样的地方:

The particular case I have before me is one where:


  1. 我有一个函数,该函数返回一个自定义集合对象(我们将其称为 FooCollection )。

  2. 此函数可以返回单个项目的集合,并且可以从源代码确定是否会发生这种情况。 (因此,我的意思是仅函数调用,而不是函数本身)

  3. 如果确实发生,则用户希望该单个项目的可能性为99.9%,而不是具有

  1. I have a function, which returns a custom collection object (we'll call it FooCollection).
  2. This function can return collections with a single item, and it can be determined from the source code whether this will happen or not. (by this I mean just the function call, not the function itself)
  3. If it does happen, it is 99.9% likely that the user wants that single item, rather than a collection with a single item.

现在,我要扔掉是否包含 FooCollection => Foo 隐藏该实现细节,但是这种转换仅在集合中只有一个项目时有效。

Now, I'm tossing up whether to include an implicit conversion from FooCollection => Foo to hide this little implementation detail, but this conversion will only work if there is a single item in the collection.

在这种情况下可以抛出 Exception 吗?还是我应该使用显式强制转换?关于如何处理此问题的其他任何想法(不,由于实现细节,我不能只使用两个函数)?

Is it ok to throw an Exception in this case? Or should I be using an explicit cast instead? Any other ideas about how I could deal with this (no, due to implementation details I can't just use two functions)?

编辑:我觉得值得注意的是, FooCollection 并没有实现任何接口,或者实际上没有扩展 Collection ,正如其名称所暗示的那样。 ,因此基于LINQ的答案是无用的。另外,尽管集合确实实现了数字索引,但它并不是处理集合的最直观的方法,因为它主要依赖命名索引。

I feel it worthy of noting that FooCollection doesn't implement any interfaces or actually extend Collection as the name might imply, hence the LINQ based answers are useless. Also, while the collection does implement a numeric index, it isn't the most intuitive way of dealing with the collection as it relies on named index mostly.

推荐答案

我遵守以下准则:您永远都不应放弃隐式转换。

I'm with the guidelines: You shouldn't ever throw from an implicit conversion.

我绝对不会在其中提供隐式转换这个案例。即使是明确的强制转换的想法也让我感到不对: Foo x =(Foo)fooCollection 似乎并不正确。

I definitely wouldn't provide an implicit conversion in this case. Even the idea of an explicit cast feels wrong to me: Foo x = (Foo)fooCollection just doesn't seem right.

为什么不让调用代码担心从 FooCollection Foo 的转换?该代码对每个人都将更加直观:

Why don't you just let the calling code worry about the conversion from FooCollection to Foo? The code would be much more intuitive for everyone:

Foo a = fooCollection[0];
Foo b = fooCollection.First();
Foo c = fooCollection.FirstOrDefault();
// etc

这篇关于是否有任何借口从隐式转换中引发Exception?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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