title: How is a null value getting into my concurrent dictionary?
body: <p>I have a simple connection manager service in my web app that keeps track of websocket connections against a user GUID they are associated with. These are stored in a dictionary with the key being a user GUID and the value being a list of connection IDs (both strings).</p>
  <pre><code>ConnectionsByUser = new ConcurrentDictionary&lt;string, List&lt;string&gt;&gt;();
  </code></pre>
  <p>I have a register connection function as below:</p>
  <pre><code>public void RegisterConnection(string userGuid, string connectionId)
  {
      if (connectionId == null)
      {
          logger.LogError($&quot;Null connection id found for user {userGuid}&quot;);
          throw new ArgumentNullException(nameof(connectionId));
      }
  
      ConnectionsByUser.AddOrUpdate(userGuid, new List&lt;string&gt;() { connectionId }, (key, value) =&gt;
      {
          value.Add(connectionId);
          return value;
      });
  }
  </code></pre>
  <p>I also have an unregister function:</p>
  <pre><code>public void UnregisterConnection(string connectionId)
  {
      foreach (var user in ConnectionsByUser)
      {
          if (user.Value.Contains(connectionId))
          {
              user.Value.Remove(connectionId);
          }
  
          if (!user.Value.Any())
          {
              ConnectionsByUser.TryRemove(user.Key, out var _);
          }
      } 
  }
  </code></pre>
  <p>Finally I have a Get function:</p>
  <pre><code>public List&lt;string&gt; GetConnectionIdsByUserGuid(string userGuid)
  {
      var hasValue = ConnectionsByUser.TryGetValue(userGuid, out var connectionIds);
  
      if (!hasValue)
      {
          return new List&lt;string&gt;();
      }
  
      return connectionIds;
  }
  </code></pre>
  <p>These are the <strong>only</strong> references to the <code>ConnectionsByUser</code> dictionary, yet somehow we have instances where <code>GetConnectionIdsByUserGuid</code> returns a list containing a null value mixed in with other actual valid connection IDs.</p>
  <p>Considering that register function has a null check, I am clueless to how a null value gets into the list, at this point I assume that there is something about the way concurrent dictionary is implemented that I do not understand.</p>