Skip to content

Using boxing ((int)(object)value) instead of pattern matching (value is int intValue) in number classes #102480

Answered by huoyaoyuan
skrysmanski asked this question in Q&A
Discussion options

You must be logged in to vote

The (ConcreteType)(object)genericVariable is a special sequence in generic context. It's specially understood by the JIT. The generic mechanism doesn't allow casting a variable typed with T to be casted to arbitrary destination type. The fake box instruction is to workaround the limitation. Even non-boxable type is also allowed with the special sequence (https://github.com/dotnet/runtime/blob/main/docs/design/features/byreflike-generics.md#special-il-sequences). With generic instantiation, no actual boxing will be performed by this special sequence.

Pattern matching actually generates more box instructions. See the example at https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBLANgHwAEAmARg…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
Answer selected by skrysmanski
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants