Skip to content

Am I using this correctly? #258

Answered by amal-stack
mwasson74 asked this question in Q&A
Discussion options

You must be logged in to vote

@mwasson74 This is happening because the compiler does not have enough information to tell that sptf.Key will not be null after calling Guard.Against.Null(). For now, you can safely suppress this warning using the null forgiving operator (!):

var thisMightBeNull = sptf!.Key

@ardalis This is possibly because Guard.Against.Null is defined as:

public static T Null<T>(this IGuardClause guardClause,
        [NotNull] [ValidatedNotNull] T input,
        [CallerArgumentExpression("input")] string? parameterName = null,
        string? message = null)

If the input parameter definition is changed from:

T input

to

T? input

the warning mentioned in the question goes away.
Since the type T does not h…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mwasson74
Comment options

Answer selected by mwasson74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants