user16077299
user16077299

Reputation:

what's the point to use NotNull attribute?

I know from C# 8, reference type are non-nullable by default, and we can turn on NRT in the project file. I saw code like this:

static void ThrowIfNull([NotNull] string? x)
{
    if (x == null)
    {
        throw new ArgumentNullException();
    }
}

I don't know what's the point to use [NotNull] in the parameter. It is like telling the compiler that:

"x is guaranteed to never be null if the method returns without throwing an exception"

But why the compiler need this information? if we do:

static void ThrowIfNull(string? x)
{
    if (x == null)
    {
        throw new ArgumentNullException();
    }
}

the code produces no warning since we do a null check

Upvotes: 4

Views: 4607

Answers (1)

DasKrümelmonster
DasKrümelmonster

Reputation: 6060

The Attribute is for the caller of the method. The C# compiler will more or less limit the scope of nullability analysis to the current method and the signatures of called methods.

In other words, if the calling method looks like this:

static void Caller()
{
  string? str = GetString();
  ThrowIfNull(str);
  Console.WriteLine(str.ToUpper());
}

there should be a compiler warning that .ToUpper() is a possible NRE. If you, however, decorate the attribute, the compiler will know that the normal return of the ThrowIfNull method implies that str is not null and the warning should be fixed.

An alternative way to propagate this info would be to make ThrowIfNull() return the string if it's not null. Then you could do this:

static void Caller()
{
  string? str = GetString();
  var str2 = ThrowIfNull(str);
  Console.WriteLine(str2.ToUpper());
}

Upvotes: 4

Related Questions