Show / Hide Table of Contents

Struct Variant

Represents the variant value on the stack.

Implements
IEquatable<Variant>
Inherited Members
ValueType.Equals(object)
ValueType.GetHashCode()
object.GetType()
object.Equals(object, object)
object.ReferenceEquals(object, object)
Namespace: DotNext.Runtime
Assembly: DotNext.dll
Syntax
public readonly ref struct Variant : IEquatable<Variant>
Remarks

This type is a counterpart of TypedReference.

Constructors

View Source

Variant(TypedReference)

Converts TypedReference to the variant value.

Declaration
[CLSCompliant(false)]
public Variant(TypedReference typedRef)
Parameters
Type Name Description
TypedReference typedRef

The typed reference to convert.

Properties

View Source

Empty

Gets empty value which IsEmpty is true.

Declaration
public static Variant Empty { get; }
Property Value
Type Description
Variant
View Source

IsEmpty

Gets a value indicating that this value is empty.

Declaration
public bool IsEmpty { get; }
Property Value
Type Description
bool
View Source

IsMutable

Gets a value indicating that the underlying value can be mutated.

Declaration
public bool IsMutable { get; }
Property Value
Type Description
bool
View Source

TargetType

Gets the type of the value.

Declaration
public Type TargetType { get; }
Property Value
Type Description
Type

Methods

View Source

Clear()

Resets the referenced value to the default value of type TargetType.

Declaration
public void Clear()
Remarks

Reference types are set to null, value types are zeroed.

Exceptions
Type Condition
InvalidOperationException

The current value is immutable or empty.

View Source

Equals(scoped Variant)

Checks whether this container references the same value and type as other.

Declaration
public bool Equals(scoped Variant other)
Parameters
Type Name Description
Variant other

The value to compare.

Returns
Type Description
bool

true, if this container references the same value and type as other; otherwise, false.

View Source

Immutable(Variant)

Creates immutable value.

Declaration
public static Variant Immutable(Variant value)
Parameters
Type Name Description
Variant value

The location of the value.

Returns
Type Description
Variant

The untyped immutable value.

View Source

Immutable<T>()

Gets the read-only reference to the underlying value.

Declaration
public ref readonly T Immutable<T>() where T : allows ref struct
Returns
Type Description
T

A location of the value.

Type Parameters
Name Description
T

The expected type of the value.

Exceptions
Type Condition
InvalidCastException

The underlying value cannot be converted to type T.

View Source

Immutable<T>(ref readonly T)

Creates an immutable value and stores its type.

Declaration
public static Variant Immutable<T>(ref readonly T location) where T : allows ref struct
Parameters
Type Name Description
T location

The location of the value.

Returns
Type Description
Variant

The untyped value.

Type Parameters
Name Description
T

The type of the value.

View Source

IsTypeOf<T>()

Determines whether the type of the referenced value is exactly T.

Declaration
public bool IsTypeOf<T>() where T : allows ref struct
Returns
Type Description
bool
Type Parameters
Name Description
T
Remarks

Unlike the is operator, this method doesn't take inheritance into account, so it returns true only if Immutable<T>() succeeds.

View Source

Mutable(ValueType)

Creates mutable boxed value and stores its type.

Declaration
public static Variant Mutable(ValueType boxedValue)
Parameters
Type Name Description
ValueType boxedValue

The boxed value.

Returns
Type Description
Variant

The boxed value.

View Source

Mutable<T>()

Tries to get the mutable reference to the underlying value.

Declaration
public ref T Mutable<T>() where T : allows ref struct
Returns
Type Description
T

A location of the value.

Type Parameters
Name Description
T

The expected type of the value.

Exceptions
Type Condition
InvalidCastException

The underlying value cannot be converted to type T; or it is not mutable.

View Source

Mutable<T>(ref T)

Creates mutable value and stores its type.

Declaration
public static Variant Mutable<T>(ref T location) where T : allows ref struct
Parameters
Type Name Description
T location

The location of the value.

Returns
Type Description
Variant

The untyped value.

Type Parameters
Name Description
T

The type of the value.

View Source

ToObject()

Tries to extract the underlying value.

Declaration
public object? ToObject()
Returns
Type Description
object

The underlying value.

Exceptions
Type Condition
NotSupportedException

The underlying value is by-ref like struct and cannot be boxed.

View Source

ToString()

Declaration
public override string? ToString()
Returns
Type Description
string
Overrides
ValueType.ToString()
View Source

TryCopyTo(scoped Variant)

Copies the value from the current location to the destination.

Declaration
public bool TryCopyTo(scoped Variant destination)
Parameters
Type Name Description
Variant destination

The destination location.

Returns
Type Description
bool

true if both memory locations of compatible types; otherwise, false.

View Source

TrySetValue(object?)

Tries to replace the referenced value.

Declaration
public bool TrySetValue(object? value)
Parameters
Type Name Description
object value

The value to set.

Returns
Type Description
bool

true if the value is replaced successfully; false if the current value is immutable or empty, or value is of incompatible type.

Remarks

The accepted value depends on TargetType:

  • For a value type, the boxed value of exactly the same type.
  • For Nullable<T>, the boxed value of the underlying type, or null.
  • For a reference type, any compatible object, or null.
  • For an unmanaged pointer type, the boxed pointer as Pointer. The type of the boxed pointer is not checked against TargetType.
  • For a function pointer type, the boxed nint.

Pointers are boxed the same way as ToObject() does, including zero pointers. By-ref-like types are not supported.

Operators

View Source

operator ==(scoped Variant, scoped Variant)

Determines whether the two containers reference the same value and type.

Declaration
public static bool operator ==(scoped Variant x, scoped Variant y)
Parameters
Type Name Description
Variant x

The first container to compare.

Variant y

The second container to compare.

Returns
Type Description
bool

true if both containers reference the same value and type; otherwise, false.

View Source

implicit operator Variant(TypedReference)

Converts TypedReference to the variant value.

Declaration
[CLSCompliant(false)]
public static implicit operator Variant(TypedReference typedRef)
Parameters
Type Name Description
TypedReference typedRef

The typed reference to convert.

Returns
Type Description
Variant

The variant points to the same location as typedRef.

View Source

operator !=(scoped Variant, scoped Variant)

Determines whether the two containers reference the different values and types.

Declaration
public static bool operator !=(scoped Variant x, scoped Variant y)
Parameters
Type Name Description
Variant x

The first container to compare.

Variant y

The second container to compare.

Returns
Type Description
bool

true if both containers reference the different values and types; otherwise, false.

Implements

IEquatable<T>

Extension Methods

BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
Enumerator.Skip<TEnumerator, T>(ref TEnumerator, int)
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾