Edytuj

Udostępnij za pośrednictwem


ObjectExtensions.TryUnbox<T>(Object, T) Method

Definition

Tries to get a boxed T value from an input Object instance.

public static bool TryUnbox<T> (this object obj, out T value) where T : struct;
static member TryUnbox : obj * 'T -> bool (requires 'T : struct)
<Extension()>
Public Function TryUnbox(Of T As Structure) (obj As Object, ByRef value As T) As Boolean

Type Parameters

T

The type of value to try to unbox.

Parameters

obj
Object

The input Object instance to check.

value
T

The resulting T value, if obj was in fact a boxed T value.

Returns

true if a T value was retrieved correctly, false otherwise.

Remarks

This extension behaves just like the following method:

public static bool TryUnbox<T>(this object obj, out T value)
{
    if (obj is T)
    {
        value = (T)obj;

        return true;
    }

    value = default;

    return false;
}

But in a more efficient way, and with the ability to also assign the unboxed value directly on an existing T variable, which is not possible with the code above.

Applies to