When would I use "NotNullIfNotNull" on a property referencing itself?
Asked Answered
D

2

7

In class Microsoft.Net.Http.Headers.ContentRangeHeaderValue, there is a nullable value type property (long?) that is decorated with a NotNullIfNotNull attribute referencing itself (property Length).

[NotNullIfNotNull(nameof(Length))]
public long? Length { get; private set; }

What is the purpose of this attribute in the context of a value type and what is the difference to simply omitting the attribute declaration?

Destroy answered 11/1, 2022 at 23:14 Comment(2)
"A return value isn't null if the input argument for the specified parameter isn't null." https://mcmap.net/q/1627497/-is-it-useful-in-a-nullable-enabled-c-environment-still-to-use-notnull-attribute-or-does-it-makes-no-difference-thenGrandiloquent
@gunr2171: In OP's example, their is no argument called Length. Likewise, the property getter might have a "return value", but the property itself doesn't. (And the property getter definitely has no argument - contrary to VB.NET, that is not supported in C#.)Torrey
A
1

According to the definition: A return value, property, or argument isn't null if the argument for the specified parameter isn't null.

The use-case scenario:

Sometimes the null state of a return value depends on the null state of one or more arguments. These methods will return a non-null value whenever certain arguments aren't null. To correctly annotate these methods, you use the NotNullIfNotNull attribute.

Examples or code snippets can be found here.

Aloisius answered 11/1, 2022 at 23:21 Comment(3)
Thank you for the link, that page has a good summary of the different nullable attributes, but it does not answer my question. I know what a NotNullIfNotNull attribute does in the context of nullable reference types and when referencing something else than the property it is declared on.Destroy
So the methods with the decorator, ENFORCE the non-nullity of the returned value. Is this correct?Nauseous
Sorry for the downvote, but this does not answer the question at all. There is no parameter called Length in OP's example, just a property. Likewise, regarding your "use-case scenario", there is no method in OP's example.Torrey
D
1

I think I slowly get an idea what problem this declaration attempts to solve:

The property could write the value it receives to a different variable than the one that is returned (using a property like this)

[NotNullIfNotNull(nameof(Length))]
public long? Length {
    get { return _length }
    private set { _anotherLength = value }
}

or simply ignore the value in the setter or do some other strange things, the attribute is needed to tell the analyzer the value given to the setter does indeed set the variable returned in the getter.

Destroy answered 11/1, 2022 at 23:56 Comment(1)
The question remains when would I use the attribute on a nullable value type? Can I then omit the call to Length.Value and simply use Length instead?Destroy

© 2022 - 2024 — McMap. All rights reserved.