Replies: 2 comments 2 replies
-
There isn't a way to do casts in the general case, because stuff like You should be able to take a reference to essentially that as a conversion function in some callsites. Compare the various key/value selector functions throughout LINQ, which are doing exactly that. |
Beta Was this translation helpful? Give feedback.
-
@dczuo hi, good question as this is suboptimal implemented in NET 7. I found the following solution: First, it needs a static TypeHelper template class that does nothing else to provide an ID, like a TypeCode, for all possible (number) types. The static constructor is only called once per type and is therefore not time-critical. Now a Cast<,> template function can use these IDs to select the conversion. Implemented as in my example, it works for all system number types in both directions without boxing. In release, checked with BenchmarkDotNet, the Cast<,> function versions are then perfectly inlined. This way it needs only one function for all cast's in difference to the NET7 implementation where every number type has a own subset of conversion template functions, where it neeads for functions like CreateTruncating two calls and inline is not always possible. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions