TryGetValue pattern with C# 8 nullable reference types : <p>I'm playing with porting some code to C# to enable nullable reference types, and I've encountered some functions in our code that use the <code>TryGetValue</code> pattern.</p>

<p>That is, something like this:</p>

<pre><code>public bool TryGetSession(string key, out Session session) {
    session = null; // assign default
    // code which looks for a session based on the key, etc
    // return true or false if we found the session key
}
</code></pre>

<p>The pattern which we're trying to express here is "if the return value is true, then <code>session</code> is non-null. If false, then don't even attempt to look at the session, it's garbage.</p>

<p>The problem is I now get a warning on <code>session = null</code>, but I'm forced to put <em>something</em> there as <code>out</code> parameters MUST be populated by the function.</p>

<p>Is there a good answer here? My thoughts:</p>

<p>I could drop the <code>TryGet</code> pattern and embrace the nullable reference types (This is what other languages like Swift seem to do) e.g. </p>

<pre><code>Session? GetSession(string key);
</code></pre>

<p>Or, I could live up to my "non-null" promise using a placeholder value e.g.</p>

<pre><code>public bool TryGetSession(string key, out Session session) {
    session = new InvalidSession(); // assign default
    ...
}
</code></pre>

<p>Are there any other options?</p>
