title: How does this ConcurrentDictionary + Lazy<Task<T>> code work?
body: <p><del>There's <a href="https://stackoverflow.com/a/53815298/30674">various</a> <a href="https://stackoverflow.com/questions/53814980/is-concurrentdictionary-getoradd-truly-thread-safe">posts</a>/<a href="https://stackoverflow.com/a/54118193/30674">answers</a> that say that the .NET/.NET Core's <code>ConcurrentDictionary</code> <code>GetOrAdd</code> method is not thread-safe when the <code>Func</code> delegate is used to calculate the value to insert into the dictionary, if the key didn't already exist.</del></p>
  <p>I'm under the belief that when using the factory method of a <code>ConcurrentDictionary</code>'s <code>GetOrAdd</code> method, it could be called multiple times &quot;at the same time/in really quick succession&quot; if a number of requests occur at the &quot;same time&quot;. This could be wasteful, especially if the call is &quot;expensive&quot;. (@panagiotis-kanavos explains this better than I). With this assumption, I'm struggling to understand how some sample code I made, seems to work.</p>
  <p>I've <a href="https://dotnetfiddle.net/viWgjA" rel="nofollow noreferrer">created a working sample on .NET Fiddle</a> but I'm stuck trying to understand how it works.</p>
  <p>A common <del>recommendation</del> suggestion/idea I've read is to have a <code>Lazy&lt;Task&lt;T&gt;&gt;</code> value in the <code>ConcurrentDictionary</code>. The idea is that the <code>Lazy</code> prevents other calls from executing the underlying method.</p>
  <p>The main part of the code which does the heavy lifting is this:</p>
  <pre><code>    public static async Task&lt;DateTime&gt; GetDateFromCache()
      {
          var result = await _cache.GetOrAdd(&quot;someDateTime&quot;, new Lazy&lt;Task&lt;DateTime&gt;&gt;(async () =&gt; 
          {
              // NOTE: i've made this method take 2 seconds to run, each time it's called.
              var someData = await GetDataFromSomeExternalDependency();
              
              return DateTime.UtcNow;
              
          })).Value;
          
          return result;
      }
  </code></pre>
  <p>This is how I read this:</p>
  <ul>
  <li>Check if <code>someDateTime</code> key exists in the dictionary.</li>
  <li>If yes, return that. &lt;-- That's a thread-safe atomic action. Yay!</li>
  <li>If no, then here we go ....</li>
  <li>Create an instance of a <code>Lazy&lt;Task&lt;DateTime&gt;&gt;</code> (which is basically instant)</li>
  <li>Return that <code>Lazy</code> instance. (so far, the actual 'expensive' operation hasn't been called, yet.)</li>
  <li>Now get the <code>Value</code>, which is a <code>Task&lt;DateTime&gt;</code>.</li>
  <li>Now <code>await</code> this task .. which finally does the 'expensive' call. It waits 2 seconds .. and then returns the result (some point in Time).</li>
  </ul>
  <p>Now this is where I'm all wrong. Because I'm assuming (above) that the value in the key/value is a <code>Lazy&lt;Task&lt;DateTime&gt;&gt;</code> ... which the <code>await</code> would call each time. If the <code>await</code> is called, one at a time (because the <code>Lazy</code> protects other callers from all calling at the same time) then I would have though that the result would a different <code>DateTime</code> with each independent call.</p>
  <p>So can someone please explain where I'm wrong in my thinking, please?</p>
  <p>(please refer to the full running code in .NET Fiddler).</p>