Open
Description
Background and motivation
Many generic types have static, non-generic, classes with a Create
helper method. This allows the compiler to infer the generic argument. StrongBox<T>
does not.
Arguably, since StrongBox<T>
is in System.Runtime.CompilerServices
, it should not be used by user code. However, it is a useful type in multithreaded code as it allows atomic updates (by nature of being heap allocated), and avoids boxing/unboxing overhead; only an allocation. The unboxing overhead can be eliminated with Unsafe.Unbox
, but, as the name suggests, it's an unsafe API.
API Proposal
namespace System.Runtime.CompilerServices;
public static class StrongBox
{
public static StrongBox<T> Create<T>(T value);
}
API Usage
Old code:
Interlocked.Exchange(ref _currentValue, new StrongBox<decimal>(newValue));
New code:
Interlocked.Exchange(ref _currentValue, StrongBox.Create(newValue));
Sure, target-typed new
could be used on the old code, but some people disable that lint if the target type is not evident.
Alternative Designs
No response
Risks
No response