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