InterlockedReference<T> Struct

Implements thread-safe management of an object reference.

public struct InterlockedReference<T> : IEquatable<InterlockedReference<T>>, IEquatable<T?> where T : class

Type parameters

T
The type of the stored reference.
Implements

Constructors

NameDescription
InterlockedReference(T?)Initializes a new instance of the InterlockedReference<T> struct.

InterlockedReference(T?)

Initializes a new instance of the InterlockedReference<T> struct.

public InterlockedReference(T? value)

Parameters

value T?
The initial value of the reference.

Properties

NameDescription
IsNullGets a value indicating whether the reference stored in an InterlockedReference<T> is null.
ValueGets or sets the reference stored in an InterlockedReference<T>. This property is thread-safe.

IsNull

Gets a value indicating whether the reference stored in an InterlockedReference<T> is null.

public bool IsNull { get; }

Property value

bool true if the reference is null; false otherwise.

Value

Gets or sets the reference stored in an InterlockedReference<T>.

This property is thread-safe.

public T? Value { get; set; }

Property value

T?

Methods

NameDescription
CompareExchange(T?, T?)Compares the value of an interlocked reference to a given comparand and, if they are equal, replaces it with a given value.
Equals(InterlockedReference<T>)Indicates whether the current object is equal to another object of the same type.
Equals(object?)Indicates whether this instance and a specified object are equal.
Equals(T?)Indicates whether the current object is equal to another object of the same type.
Exchange(T?)Sets the value of an interlocked reference.
GetHashCode()Returns the hash code for this instance.
ToString()Returns the fully qualified type name of this instance.

CompareExchange(T?, T?)

Compares the value of an interlocked reference to a given comparand and, if they are equal, replaces it with a given value.

public T? CompareExchange(T? value, T? comparand)

Parameters

value T?
The value that replaces the destination value if the comparison by reference results in equality.
comparand T?
The value that is compared by reference to the value of this instance.

Returns

T? The original value of this instance.

Equals overloads

Equals(InterlockedReference<T>)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(InterlockedReference<T> other)
Parameters
other InterlockedReference<T>
An object to compare with this object.
Returns

bool true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)
Parameters
obj object?
The object to compare with the current instance.
Returns

bool true if obj and this instance are the same type and represent the same value; otherwise, false.

Equals(T?)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(T? other)
Parameters
other T?
An object to compare with this object.
Returns

bool true if the current object is equal to the other parameter; otherwise, false.

Exchange(T?)

Sets the value of an interlocked reference.

public T? Exchange(T? value)

Parameters

value T?
The value to set.

Returns

T? The previous value of this instance.

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.

ToString()

Returns the fully qualified type name of this instance.

public override string ToString()

Returns

string The fully qualified type name.

Operators

NameDescription
operator ==(InterlockedReference<T>, InterlockedReference<T>)Determines whether two specified interlocked references have the same value.
operator ==(InterlockedReference<T>, T?)Determines whether the value of a specified interlocked reference is the same as a specified value.
operator !=(InterlockedReference<T>, InterlockedReference<T>)Determines whether two specified interlocked references have different values.
operator !=(InterlockedReference<T>, T?)Determines whether the value of a specified interlocked reference is different from a specified value.

op_Equality overloads

operator ==(InterlockedReference<T>, InterlockedReference<T>)

Determines whether two specified interlocked references have the same value.

public static bool operator ==(InterlockedReference<T> a, InterlockedReference<T> b)
Parameters
a InterlockedReference<T>
The first instance to compare.
b InterlockedReference<T>
The second instance to compare.
Returns

bool true if the value of a is the same as the value of b; otherwise, false.

operator ==(InterlockedReference<T>, T?)

Determines whether the value of a specified interlocked reference is the same as a specified value.

public static bool operator ==(InterlockedReference<T> a, T? b)
Parameters
a InterlockedReference<T>
The interlocked reference to compare.
b T?
The reference to compare.
Returns

bool true if the value of a is the same as the value of b; otherwise, false.

op_Inequality overloads

operator !=(InterlockedReference<T>, InterlockedReference<T>)

Determines whether two specified interlocked references have different values.

public static bool operator !=(InterlockedReference<T> a, InterlockedReference<T> b)
Parameters
a InterlockedReference<T>
The first instance to compare.
b InterlockedReference<T>
The second instance to compare.
Returns

bool true if the value of a is different from the value of b; otherwise, false.

operator !=(InterlockedReference<T>, T?)

Determines whether the value of a specified interlocked reference is different from a specified value.

public static bool operator !=(InterlockedReference<T> a, T? b)
Parameters
a InterlockedReference<T>
The interlocked reference to compare.
b T?
The reference to compare.
Returns

bool true if the value of a is different from the value of b; otherwise, false.