Skip to content

NullableExtensions.DangerousGetValueOrNullReference() #61

@rickbrew

Description

@rickbrew

The existing DangerousGetValueOrDefaultReference() is useful, but for a lot of my interop code I need null when HasValue is false

I'm actually using a more dangerous version in my code base that returns a pointer, but @Sergio0694 says return a null ref is a better idea, and I agree. Can always Unsafe.AsPointer() from that.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe ref T DangerousGetValueOrNullReference<T>(ref this T? maybe)
    where T : unmanaged
{
    return ref (maybe.HasValue ? ref maybe.DangerousGetValueOrDefaultReference() : ref Unsafe.NullRef<T>());
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request 📬A request for new changes to improve functionalityhigh-performance 🚂Issues/PRs for the HighPerformance package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions