In some library I create I have to use following cast:
public void Foo(IList<uint> uintsList) // I need to use uint in the interface for this method
{
List<double> doublesList = uintsList.Cast<double>().ToList();
// Do something with the doublesList
}
I assumed that cast uint
-> double
should be always valid, and during my test it always worked fine.
But in the application, which uses this method the InvalidCastException
occured. Unfortunately I do not have access to this application. So here are my questions:
- What could cause this exception occured? Isn't the cast uint->double always valid?
- How can I secure my algorithm to avoid this exception?
EDIT
Of course, before casting I always perform check to avoid situation when uintsList
is null or empty
EDIT 2
OK, the problem is solved, I changed the cast using the ConvertAll
method, but still I don't understand how it could happen?
So this question still bothers me: how the same part of the code could run properly at my computer, and throw an exception at another? Different compiler/environment versions? Some specific settings? Can anyone tell me where should I seek for the reasons of this situation to avoid it in the future?