Class NullableHelper
Provides helper extension methods for working with nullable types.
public static class NullableHelper
- Inheritance
-
NullableHelper
- Inherited Members
Methods
Convert<T, TResult>(T, Func<T, TResult>, Func<TResult>)
Converts the value to a result type using the appropriate conversion function based on whether the value is null.
public static TResult Convert<T, TResult>(this T value, Func<T, TResult> notNullFunc, Func<TResult> nullFunc) where T : class
Parameters
value
TThe input value to convert.
notNullFunc
Func<T, TResult>A function to convert the value when it is not null.
nullFunc
Func<TResult>A function to produce a result when the value is null.
Returns
- TResult
The result of applying either
notNullFunc
ornullFunc
to the value.
Type Parameters
T
The type of the input value. Must be a reference type.
TResult
The type of the result.
Exceptions
- ArgumentNullException
Thrown when
notNullFunc
ornullFunc
is null.
DefaultAsNull<T>(T)
Returns the default value as null if the value is equal to its default; otherwise, returns the value as a nullable type.
public static T? DefaultAsNull<T>(this T value) where T : struct
Parameters
value
TThe value to check.
Returns
- T?
Null if the value is the default of its type; otherwise, the value wrapped as a nullable type.
Type Parameters
T
The value type.
GetUnderlyingType(Type)
Gets the underlying type argument of the specified nullable type.
public static Type GetUnderlyingType(this Type nullableType)
Parameters
nullableType
TypeThe nullable type to get the underlying type from.
Returns
- Type
The underlying type if the provided type is nullable; otherwise, null.
IsNull<T>(T)
Determines whether the specified value is null. For value types, this method returns false.
public static bool IsNull<T>(this T value)
Parameters
value
TThe value to check for null.
Returns
- bool
True if the value is null; otherwise, false.
Type Parameters
T
The type of the value.
IsNull<T>(T, bool)
Determines whether the specified value is null or its default for value types.
public static bool IsNull<T>(this T value, bool checkValueTypeOnDefault)
Parameters
value
TThe value to check.
checkValueTypeOnDefault
boolIf true and the type is a value type, the value is compared to its default value.
Returns
- bool
True if the reference type value is null or if the value type is equal to its default value when
checkValueTypeOnDefault
is true; otherwise, false.
Type Parameters
T
The type of the value.
IsNullable(Type)
Determines whether the specified type is a nullable type.
public static bool IsNullable(this Type type)
Parameters
type
TypeThe type to inspect.
Returns
- bool
True if the type is nullable; otherwise, false.
Exceptions
- ArgumentNullException
Thrown when
type
is null.
MakeNullable(Type)
Creates a nullable type from the given type.
public static Type MakeNullable(this Type type)
Parameters
type
TypeThe type to make nullable.
Returns
- Type
A nullable type of the given type.
TryMakeNullable(Type)
Converts the given type to a nullable type if it is a non-nullable value type.
public static Type TryMakeNullable(this Type type)
Parameters
type
TypeThe type to convert.
Returns
- Type
The nullable version of the type if it is a value type and not already nullable; otherwise, the original type.