title: C# 8 validate arguments with utility method
body: <p>Say I have a method that accepts a <a href="https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references" rel="nofollow noreferrer">nullable reference type</a> like:</p>
  
  <pre class="lang-cs prettyprint-override"><code>int Func(string? param)
  {
      return param.Length;
  }
  </code></pre>
  
  <p>The compiler correctly warns that <code>param</code> should be checked for <code>null</code>.</p>
  
  <p>Before C# 8, I was used to validating arguments with external utility functions like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static class Validate
  {
     public static void NotNull(object? value, string parameterName, string? field = null)
     {
       if (value == null) throw new ArgumentNullException(parameterName + (field == null ? string.Empty : "." + field));
     }
  }
  … 
  int Func(string? param)
  {
      Validate.NotNull(param, nameof(param));
      return param.Length;
  }
  </code></pre>
  
  <p>In this case, it seems that the compiler can't detect that argument <code>param</code> is actually checked for <code>null</code> inside of the <code>Validate</code> method, and thus still complains.</p>
  
  <p>Is there no other way to directly test for <code>null</code> values inside of the method?</p>
  
  <pre class="lang-cs prettyprint-override"><code>int Func(string? param)
  {
      if (value == null) throw new ArgumentNullException(nameof(param));
      return param.Length;
  }
  </code></pre>