Struct NullableObject<T>
Represents a wrapper for an object that can be null.
public readonly struct NullableObject<T> : IEquatable<NullableObject<T>>, IEquatable<T>
Type Parameters
T
The type of the object.
- Implements
-
IEquatable<T>
- Inherited Members
- Extension Methods
Constructors
NullableObject(T?)
Initializes a new instance of the NullableObject<T> struct.
public NullableObject(T? item)
Parameters
item
TThe item to wrap.
Properties
IsNull
Gets a value indicating whether the wrapped item is null.
public bool IsNull { get; }
Property Value
Item
Gets the wrapped item.
public T? Item { get; }
Property Value
- T
Null
Gets a NullableObject<T> that represents a null value.
public static NullableObject<T> Null { get; }
Property Value
Remarks
If T
is a struct that is not wrapped in Nullable<T>,
this property will return a NullableObject<T> with a non-null default value
because structs cannot be null unless wrapped in Nullable<T>.
Methods
Equals(NullableObject<T>)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(NullableObject<T> other)
Parameters
other
NullableObject<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?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
obj
objectThe object to compare with the current object.
Returns
- bool
true if the specified object is equal to the current object; otherwise, false.
Remarks
If you compare two NullableObject<T> instances with different generic types and both are null, the Equals(object?) method will return false because they are considered two different null values. There is a type match check.
Equals(T?)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(T? other)
Parameters
other
TAn object to compare with this object.
Returns
- bool
true if the current object is equal to the
other
parameter; otherwise, false.
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
ToString()
Returns a string that represents the current object.
public override string? ToString()
Returns
- string
A string that represents the current object.
Operators
operator ==(NullableObject<T>, NullableObject<T>)
Determines whether two specified instances of NullableObject<T> are equal.
public static bool operator ==(NullableObject<T> left, NullableObject<T> right)
Parameters
left
NullableObject<T>The first object to compare.
right
NullableObject<T>The second object to compare.
Returns
- bool
true if the left and right parameters are equal; otherwise, false.
implicit operator T?(NullableObject<T>)
Performs an implicit conversion from NullableObject<T> to T
.
public static implicit operator T?(NullableObject<T> nullObject)
Parameters
nullObject
NullableObject<T>The NullableObject<T> to convert.
Returns
- T
implicit operator NullableObject<T>(T?)
Performs an implicit conversion from T
to NullableObject<T>.
public static implicit operator NullableObject<T>(T? item)
Parameters
item
TThe item to convert.
Returns
operator !=(NullableObject<T>, NullableObject<T>)
Determines whether two specified instances of NullableObject<T> are not equal.
public static bool operator !=(NullableObject<T> left, NullableObject<T> right)
Parameters
left
NullableObject<T>The first object to compare.
right
NullableObject<T>The second object to compare.
Returns
- bool
true if the left and right parameters are not equal; otherwise, false.