Struct Any
- Namespace
- Badeend
- Assembly
- Badeend.Any.dll
Any
is able to hold any value of any type (hence the name).
This is similar to assigning a value to a variable of type object
.
The key difference is that converting to Any
does not cause
boxing for many small struct types as they're stored inline in the Any
instance itself. Whereas converting a struct type to object
will
always box.
public readonly struct Any : IEquatable<Any>
- Implements
- Inherited Members
- Extension Methods
Remarks
Boxing is avoided for any value type that:
- is 8 bytes in size or less, and:
- does not contain references.
In practice this turns out to be many of the commonly used .NET built-ins:
bool
char
byte
sbyte
short
ushort
int
uint
long
ulong
IntPtr
/nint
UIntPtr
/nuint
float
double
DateTime
TimeSpan
DateOnly
TimeOnly
Index
Range
- all
enum
s ValueTuple
s such as(int, int)
Tip
Any
provides conversion operators for these (and more) BCL types;
implicit operators for types that are known to be inlinable, and
explicit operators for types that that require boxing.
Due to the large amount of them, they're not shown in the documentation.
Any has a built-in representation for null
, which can
be checked for using HasValue. Because Any
can already
be "null" by itself, there's typically no need for having a nullable Any
(Any?
/ Nullable<Any>
), as that will result in
"double" nullability.
The default
value of Any
is null
.
Properties
HasValue
Returns true
if the value is not null
.
[Pure]
public bool HasValue { get; }
Property Value
Type
Returns the Type of the contained
value or null
if the value is null
.
[Pure]
public Type? Type { get; }
Property Value
Remarks
This never returns typeof(Any)
.
Methods
BitwiseEquals(Any, Any)
Check if the two Any instances are identical.
This is checked by comparing the object references to the heap data and the raw bitpattern of the inlined struct data (if any).
It does not call the inner value's .Equals
implementation.
[Pure]
public static bool BitwiseEquals(Any a, Any b)
Parameters
Returns
Remarks
This is the spiritual equivalent of Object.ReferenceEquals(a, b)
,
but for Any
instances.
Create<T>(T)
Wrap the provided value
inside a new Any,
avoiding boxing whenever possible.
Boxing can be prevented for any value type that:
- is 8 bytes in size or less, and:
- does not contain references.
[Pure]
public static Any Create<T>(T value)
Parameters
value
T
Returns
Type Parameters
T
Equals(Any)
Call the inner value's .Equals
implementation.
Which .Equals
method is called depends on the runtime values of this
and other
:
- If both are
null
:true
- If only one is
null
:false
- If both are inlined structs of the same type:
EqualityComparer<T>.Default.Equals(this, other)
- If both are inlined structs, but of different types:
false
- If
this
is heap allocated andother
is inlined:other.Equals((object)this)
- All other cases:
this.Equals((object)other)
[Pure]
public bool Equals(Any other)
Parameters
other
Any
Returns
GetHashCode()
Invoke GetHashCode() on the inner value.
[Pure]
public override int GetHashCode()
Returns
ToObject()
Box the inner value, if it wasn't already.
[Pure]
public object? ToObject()
Returns
ToString()
Invoke ToString() on the inner value.
[Pure]
public override string? ToString()
Returns
TryGetValue<T>(out T)
Attempt to downcast to the specified type T
.
If the current instance represents null
, the attempt always
returns false
.
public bool TryGetValue<T>(out T value) where T : notnull
Parameters
value
TOn success; the downcast value.
Returns
- bool
true
is the conversion succeeded. Otherwisefalse
.
Type Parameters
T
The type to downcast to.
Remarks
For more ergonomic access, you might be interested in the
.As<T>()
, .Is<T>()
and .Cast<T>()
extension methods.
Exceptions
- InvalidOperationException
Type parameter
T
is a nullable value type.
Operators
operator ==(Any, Any)
Check for equality using the inner value's .Equals
implementation.
See Equals(Any) for more information.
[Pure]
public static bool operator ==(Any left, Any right)
Parameters
Returns
operator !=(Any, Any)
Check for inequality using the inner value's .Equals
implementation.
See Equals(Any) for more information.
[Pure]
public static bool operator !=(Any left, Any right)