.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
Add API to get just nullable Box instance without success flag and works faster.
API breakdown
namespace CommunityToolkit.HighPerformance;
public sealed class Box<T>
{
/// <summary>
/// Tries to get a nullable <see cref="Box{T}"/> reference from an input <see cref="object"/> representing a boxed <typeparamref name="T"/> value.
/// </summary>
/// <param name="obj">The input <see cref="object"/> instance to check.</param>
/// <returns><see cref="Box{T}"/> instance if it was retrieved correctly, otherwise see langword="null"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Box<T>? TryGetFrom(object obj) => obj.GetType() == typeof(T) ? Unsafe.As<Box<T>>(obj)! : null;
}
Usage example
public class Some
{
private Box<some>? box;
public Some (object value)
{
box = Box<some>.TryGetFrom(value); // not Box<some>.TryGetFrom(value, out box);
}
}
Overview
Add API to get just nullable Box instance without success flag and works faster.
API breakdown
Usage example
Breaking change?
No
Alternatives
I am not sure.
Additional context
No response
Help us help you
Yes, I'd like to be assigned to work on this item