Struct NullableRef<T>
Represents a explicitly nullable reference
public readonly ref struct NullableRef<T> : IEquatable<NullableRef<T>>, IEquatable<NullableRefReadOnly<T>>
Type Parameters
TThe type of value the NullableRef<T> represents a reference of
- Implements
- Inherited Members
Remarks
To construct a NullableRef<T> that represents a null-reference, use Null or default(NullableRef<.
T>)
Constructors
NullableRef(ref T)
Represents a explicitly nullable reference
public NullableRef(ref T target)
Parameters
targetTAn actual reference to the value of type
Tthe NullableRef<T> should represent a reference to
Remarks
To construct a NullableRef<T> that represents a null-reference, use Null or default(NullableRef<.
T>)
Properties
HasTarget
Gets a value indicating whether this NullableRef<T> does not represent a null-reference and the Target property can be safely dereferenced
public bool HasTarget { get; }
Property Value
- bool
A value indicating whether this NullableRef<T> does not represent a
null-reference
IsNull
Gets a value indicating whether this NullableRef<T> does represent a null-reference
public bool IsNull { get; }
Property Value
- bool
A value indicating whether this NullableRef<T> does represent a
null-reference
Remarks
Null
Gets a NullableRef<T> that represents a null-reference
public static NullableRef<T> Null { get; }
Property Value
- NullableRef<T>
A NullableRef<T> that represents a
null-reference
Remarks
Do not try the dereference the returned reference from a call to GetReferenceOrNull() on the resulting value of this property!
Target
Gets an actual reference to the value of type T this NullableRef<T> references
public ref T Target { get; }
Property Value
- T
An actual reference to the value of type
Tthis NullableRef<T> references
Remarks
If the value of the HasTarget property is false or the value of the IsNull property is true,
accessing this property will throw an InvalidOperationException.
Exceptions
- InvalidOperationException
The NullableRef<T> represents a
null-reference
Methods
Equals(NullableRefReadOnly<T>)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(NullableRefReadOnly<T> other)
Parameters
otherNullableRefReadOnly<T>An object to compare with this object.
Returns
Equals(NullableRef<T>)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(NullableRef<T> other)
Parameters
otherNullableRef<T>An object to compare with this object.
Returns
Equals(object?)
Calls to this method are not supported
[Obsolete("Calls to this method are not supported. This method will always throw an exception. Use the \"Equals\" methods or the equality operators instead.")]
[DoesNotReturn]
public override bool Equals(object? obj)
Parameters
objobjectNot supported
Returns
- bool
Not supported
Exceptions
- NotSupportedException
always
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
GetReferenceOrNull()
Gets an actual reference to the value of type T this NullableRef<T> references, without validating if it's safely dereferencable
public ref readonly T GetReferenceOrNull()
Returns
- T
An actual reference to the value of type
Tthis NullableRef<T> references
Remarks
This method does not validate if the represented reference is safely dereferencable! Users should check the value of the HasTarget property or the value of the IsNull property first, to determine if the result of this method is safe to dereference. Use with care!
Operators
operator ==(NullableRef<T>, NullableRefReadOnly<T>)
Compares two values to determine equality.
public static bool operator ==(NullableRef<T> left, NullableRefReadOnly<T> right)
Parameters
leftNullableRef<T>The value to compare with
right.rightNullableRefReadOnly<T>The value to compare with
left.
Returns
operator ==(NullableRef<T>, NullableRef<T>)
Compares two values to determine equality.
public static bool operator ==(NullableRef<T> left, NullableRef<T> right)
Parameters
leftNullableRef<T>The value to compare with
right.rightNullableRef<T>The value to compare with
left.
Returns
implicit operator NullableRefReadOnly<T>(NullableRef<T>)
Converts a NullableRef<T> to a NullableRefReadOnly<T>
public static implicit operator NullableRefReadOnly<T>(NullableRef<T> nullableRef)
Parameters
nullableRefNullableRef<T>The NullableRef<T> to convert
Returns
Remarks
The resulting NullableRefReadOnly<T> will reference the same value of T nullableRef references.
This also means that, if nullableRef represents a null-reference, the resulting NullableRefReadOnly<T> will represent a null-reference.
operator !=(NullableRef<T>, NullableRefReadOnly<T>)
Compares two values to determine inequality.
public static bool operator !=(NullableRef<T> left, NullableRefReadOnly<T> right)
Parameters
leftNullableRef<T>The value to compare with
right.rightNullableRefReadOnly<T>The value to compare with
left.
Returns
operator !=(NullableRef<T>, NullableRef<T>)
Compares two values to determine inequality.
public static bool operator !=(NullableRef<T> left, NullableRef<T> right)
Parameters
leftNullableRef<T>The value to compare with
right.rightNullableRef<T>The value to compare with
left.