Class AnyValueExtensions
- Namespace
- Badeend
- Assembly
- Badeend.Any.dll
Extension methods for Any constrained to where T : struct
.
public static class AnyValueExtensions
- Inheritance
-
AnyValueExtensions
- 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:
int? x = obj as int?;
// Using Any:
int? x = any.As<int>();
[Pure]
public static T? As<T>(this Any any) where T : struct
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:
int? x = (int?)obj;
// Using Any:
int? x = any.CastNullable<int>();
public static T? CastNullable<T>(this Any any) where T : struct
Parameters
any
Any
Returns
- T?
Type Parameters
T
Remarks
Alternatively to calling this method, for many of the .NET built-in value
types you can use regular cast syntax instead, e.g. (int?)myAny
.
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:
// Using objects:
int x = (int)obj;
// Using Any:
int x = any.Cast<int>();
public static T Cast<T>(this Any any) where T : struct
Parameters
any
Any
Returns
- T
Type Parameters
T
Remarks
Alternatively to calling this method, for many of the .NET built-in value
types you can use regular cast syntax instead, e.g. (int)myAny
.
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 int) { /* ... */ }
// Using Any:
if (any.Is<int>()) { /* ... */ }
[Pure]
public static bool Is<T>(this Any any) where T : struct
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 int x) { /* ... */ }
// Using Any:
if (any.Is(out int x)) { /* ... */ }
public static bool Is<T>(this Any any, out T value) where T : struct
Parameters
any
Anyvalue
T
Returns
Type Parameters
T