1

Say I have a method that starts like this, where we use a method to check for null and other validations.

If I do that, the compiler still complains about a possible null reference exception in the lines of code that follow.

Is there a way to make it so it doesn't?

For instance, using String.IsNullOrEmpty or String.IsNullOrWhitespace seems to indicate to the compiler that the value has been checked.

Can I do that with my own methods?

public static void Apply(string imagePath, string texturePath, string outputPath, bool isPreviewImage, double? dpi, Color distressedColor)
{
    // check each param for not null
    StringParameter.ThrowIfNoValue(imagePath, nameof(imagePath));
    StringParameter.ThrowIfNoValue(texturePath, nameof(texturePath));
    StringParameter.ThrowIfNoValue(outputPath, nameof(outputPath));

    // how do I make the compiler understand that "imagePath" cannot be null
    var request = WebRequest.Create(imagePath);
}

static class StringParameter
{
    // is there a way to mark this method to the compiler so it understands that the value cannot be null when use beyond this method call?
    internal static void ThrowIfNoValue(string value, string name)
    {
        if (string.IsNullOrWhiteSpace(value ?? throw new ArgumentNullException(name)))
            throw new ArgumentException("Must have a value", name);
    }
}
Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
Ristogod
  • 905
  • 4
  • 14
  • 29
  • Does this answer your question? [Possibility of external functions as nullable guards?](https://stackoverflow.com/questions/58411013/possibility-of-external-functions-as-nullable-guards) – canton7 Feb 10 '20 at 15:48
  • Yes, that is the intended behavior. See links in my linked answer. – canton7 Feb 10 '20 at 16:03
  • 1
    @Silvermind As I said in that other answer, `[NotNull]` on an input parameters means "*For inputs (by-value/in parameters) the value passed is known not to be null when we return.*". So when `ThrowIfNoValue` returns, `value` is known to be not null. – canton7 Feb 10 '20 at 16:09
  • @canton7 I'm sorry. I believe you and I see that the docs explain it, but I find that information crucial enough to be contained in intellisense. Thanks for your patience though. – Silvermind Feb 10 '20 at 16:14
  • @Silvermind Then I suggest you open an issue in the appropriate place. I'm not in charge of intellisense... – canton7 Feb 10 '20 at 16:17
  • @Silvermind In fact, the docs do say "*Specifies that an input argument was not null when the call returns.*" – canton7 Feb 10 '20 at 16:19
  • 2
    I've added this information (behavior of `[NotNull]` on input parameters) to the docs recently. See https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.notnullattribute?view=netcore-3.1 (PR https://github.com/dotnet/dotnet-api-docs/pull/3782) – Julien Couvreur Feb 10 '20 at 21:07
  • @canton7 That's what I meant when I said that the docs explain it. I'm not asking you to change intellisense. Don't assume I meant you should feel responsible for intellisense. – Silvermind Feb 10 '20 at 23:24

0 Answers0