NullableHelper

Ecng.Common

Provides helper extension methods for working with nullable types.

方法

Convert``2
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.

返回值: The result of applying either or to the value.

DefaultAsNull``1
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.

返回值: Null if the value is the default of its type; otherwise, the value wrapped as a nullable type.

GetUnderlyingType
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.

返回值: The underlying type if the provided type is nullable; otherwise, null.

IsNullable
public static bool IsNullable(Type type)
result = NullableHelper.IsNullable(type)

Determines whether the specified type is a nullable type.

type
The type to inspect.

返回值: True if the type is nullable; otherwise, false.

IsNull``1
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.

返回值: True if the value is null; otherwise, false.

IsNull``1
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.

返回值: True if the reference type value is null or if the value type is equal to its default value when is true; otherwise, false.

MakeNullable
public static Type MakeNullable(Type type)
result = NullableHelper.MakeNullable(type)

Creates a nullable type from the given type.

type
The type to make nullable.

返回值: A nullable type of the given type.

TryMakeNullable
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.

返回值: The nullable version of the type if it is a value type and not already nullable; otherwise, the original type.