Open sandboxFocus

Method WithNullForgivingOperator

WithNullForgivingOperator(IExpression, bool)

Returns an expression with the null-forgiving operator (!) applied, suppressing nullable warnings.

Declaration
public static IExpression WithNullForgivingOperator(this IExpression expression, bool force = false)
Parameters
Type Name Description
IExpression expression

The expression to which the null-forgiving operator should be applied.

bool force

When false (the default), the operator is only added if the expression is nullable. When true, the operator is always added regardless of the expression's nullability.

Returns
Type Description
IExpression

An expression with the null-forgiving operator applied, or the original expression if the operator is not needed and force is false.

Remarks

The null-forgiving operator tells the compiler that an expression is not null, even if the compiler's static analysis cannot determine this. Use this method when you know an expression will not be null at run time but the compiler cannot verify this.

The operator is never added to value types (except Nullable<T>) since they cannot be null by definition.