Skip to content

[API Proposal]: StrongBox.Create #105400

Open
@colejohnson66

Description

@colejohnson66

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Runtime.CompilerServicesneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions