Class AnyReferenceExtensions
- Namespace
- Badeend
- Assembly
- Badeend.Any.dll
Extension methods for Any constrained to where T : class
.
public static class AnyReferenceExtensions
- Inheritance
-
AnyReferenceExtensions
- Inherited Members
Methods
As<T>(Any)
Attempt to cast the inner value to the specified type T
.
If the cast isn't possible, null
is returned.
This is the equivalent of the C# as
operator:
// Using objects:
string? x = obj as string;
// Using Any:
string? x = any.As<string>();
[Pure]
public static T? As<T>(this Any any) where T : class
Parameters
any
Any
Returns
- T
Type Parameters
T
CastNullable<T>(Any)
Attempt to cast the inner value to the specified type T
.
If the inner value is null, this method returns null too.
If the inner value is not null but the cast isn't possible,
an exception will be thrown.
This is the equivalent of the C# cast expression:
// Using objects:
string? x = (string?)obj;
// Using Any:
string? x = any.CastNullable<string>();
public static T? CastNullable<T>(this Any any) where T : class
Parameters
any
Any
Returns
- T
Type Parameters
T
Exceptions
- InvalidCastException
The inner value is not of type
T
.
Cast<T>(Any)
Attempt to cast the inner value to the specified type T
.
If the cast isn't possible, an exception will be thrown.
This is the equivalent of the C# cast expression followed by a null check:
// Using objects:
string x = (string?)obj ?? throw new NullReferenceException();
// Using Any:
string x = any.Cast<string>();
public static T Cast<T>(this Any any) where T : class
Parameters
any
Any
Returns
- T
Type Parameters
T
Exceptions
- InvalidCastException
The inner value is not of type
T
.- NullReferenceException
The inner value is null.
Is<T>(Any)
Check if the value is of the specified type T
.
This is the equivalent of the C# is
operator:
// Using objects:
if (obj is string) { /* ... */ }
// Using Any:
if (any.Is<string>()) { /* ... */ }
[Pure]
public static bool Is<T>(this Any any) where T : class
Parameters
any
Any
Returns
Type Parameters
T
Is<T>(Any, out T)
Check if the value is of the specified type T
cast to that type if it is.
This is the equivalent of the C# is
operator:
// Using objects:
if (obj is string x) { /* ... */ }
// Using Any:
if (any.Is(out string x)) { /* ... */ }
public static bool Is<T>(this Any any, out T value) where T : class
Parameters
any
Anyvalue
T
Returns
Type Parameters
T