title: Assert Variable is not Null
body: <p>I have a Variable with type <code>DateTime?</code></p>
  
  <p>In a Function I check it for being <code>null</code> and want to use it afterwards without always having to <code>?.</code> every call. In e.g. Kotlin the IDE recognizes a check like that and asserts that the variable cannot be <code>null</code> afterwards. Is there a way to do this in C#?</p>
  
  <pre><code>DateTime? BFreigabe = getDateTime();
  if (BFreigabe == null) return false;
  TimeSpan span = BFreigabe - DateTime.Now; 
  //Shows Error because it.BFreigabe has the type DateTime?, even though it can't be null
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>When using </p>
  
  <pre><code>TimeSpan span = BFreigabe.Value - DateTime.Now;
  </code></pre>
  
  <p>instead it works <strong>in this case</strong> because <code>.Value</code> doesn't have nullsafety at all. However, considering that this would compile even without the null check and just produce an error, the general question still remains. How can one persuade C# that a former nullable variable isn't nullable any more?</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>Casting DateTime on the Variable works. </p>
  
  <pre><code>TimeSpan span = (DateTime)BFreigabe - DateTime.Now;
  </code></pre>
  
  <p>Still not as safe as in Kotlin, but similar enough.</p>