Is there ever an excuse to exclude an exception from an implicit conversion?

From MSDN :

By eliminating unnecessary casts, implicit conversions can improve the readability of source code. However, since implicit conversions may occur without instructions from programmers, 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 safely used without the knowledge of the programmer. If the conversion operator cannot fulfill these criteria, it must be explicitly marked.

While I disagree with any particular point, and I agree that all this is very good, is there a reason that is large enough to justify the violation of some implicit transformations without throwing exceptions?

A special case that I have is one where:

  • I have a function that returns a custom collection object (we will call it FooCollection ).
  • This function can return collections with a single element, or it can be determined from the source code whether this will happen or not . (by this I mean only calling the function, not the function itself)
  • If this is true, then it is a 99.9% chance that the user will want one item, not a collection with one item.

Now I throw up whether to include the implicit conversion from FooCollection => Foo to hide this small implementation detail, but this conversion will only work if there is one element in the collection.

Is it possible to throw an Exception in this case? Or should I use explicit conversion instead? Any other ideas on how I can handle this (no, due to implementation details I can't just use two functions)?

EDIT: I find it worth noting that FooCollection does not implement any interfaces or actually extends Collection , as the name might imply, so LINQ-based answers are useless. In addition, although a collection does implement a numerical index, it is not the most intuitive way to work with a collection, since it mainly uses a named index.

+6
casting explicit c # implicit
source share
4 answers

I am with recommendations: you should not throw out of implicit conversion.

In this case, I definitely will not suggest implicit conversion. Even the idea of ​​an explicit throw seems wrong to me: Foo x = (Foo)fooCollection just doesn't seem right.

Why don't you just let the caller worry about converting from FooCollection to Foo ? The code would be much more understandable to everyone:

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

This is clearly not normal. Never use exceptions to implement logic!

You can use Linq-Statement FooCollection.FirstOrDefault() , which will give null or the first element.

+1
source share

The cast must be explicit. It will be very strange to be able to assign a FooCollection Foo without at least an actor.

This suggests that IMHO is not a good way to do this. Even if you say no, I will use the function because, even if you do not have control over the implementation of these classes, you can at least add extension methods such as Foo ToFoo(this FooCollection collection) to complete the task.

0
source share

Implicit copnversion that only works if there is only 1 element in the collection? So, in fact, it doesn’t work most of the time?

I would never make this conversion implicit. Stick clearly. If the programmer using your function wants to have a single element, he should just specify your class.

0
source share

All Articles