What's the purpose of StrongBox<T>? #47774
-
While digging through The reason why I ask is also this article: https://stackoverflow.com/questions/13612098/usage-of-concurrentqueuestrongboxt that made things even worse. Here some author say that it improved performance of some code of a company he worked for when switching from a direct reference to a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Used by C++/CLI? It has syntax to define reference of value types, like |
Beta Was this translation helpful? Give feedback.
-
It let's you "box" a value type to the heap but in a strongly-typed manner, e.g. instead of doing: object box = 42;
... // some time later
int i = (int)box; and then being able to pass around the weakly-typed var box = new StrongBox<int>(42);
... // some time later
int i = box.Value; and then pass around You can see example uses of it here: Some typical (advanced) use cases include:
|
Beta Was this translation helpful? Give feedback.
It let's you "box" a value type to the heap but in a strongly-typed manner, e.g. instead of doing:
and then being able to pass around the weakly-typed
box
, you can do:and then pass around
box
and access itsValue
, which is strongly typed asint
(via the genericT
). Hence the name "strong box", i.e. "strongly-typed box". (It's not limited for use with value types, though.)You can see example uses of it here:
https://source.dot.net/#System.Private.CoreLib/StrongBox.cs,bdbb335cbbdee41b,references
Some typical (advanced) use cases include: