title: Data structure to use when gathering response from async calls
body: <p>I am running this piece of code in my application.</p>
  
  <pre><code>public Task&lt;BulkResponse&lt;JObject&gt;&gt; GetRelatedObjectsAsync(IEnumerable&lt;PrimaryObjectInfo&gt; primaryObjectInfos)
  {
      var allSecondaries = new List&lt;Tuple&lt;int, List&lt;JObject&gt;&gt;&gt;();
      var exceptionsDict = new ConcurrentDictionary&lt;int, Exception&gt;();
  
      var relatedObjectsTasks = primaryObjectInfos.Select(async primaryObjectInfo =&gt;
      {
          try
          {
              var secondaryObject = await objectManager.GetRelatedObjectsAsync(primaryObjectInfo);
              allSecondaries.Add(Tuple.Create(primaryObjectInfo.Index, secondaryObject.ToList()));
          }
          catch (Exception ex)
          {
              exceptionsDict.TryAdd(primaryObjectInfo.Index,  ex);
          }
      });
  
      await Task.WhenAll(relatedObjectsTasks);
  
      return ConvertToBulkResponse(allSecondaries, exceptionsDict);
  }
  </code></pre>
  
  <p>When I run this code <code>allSecondaries</code> object sometimes returns valid list of results and sometimes the code ends up catching exceptions for the parallel threads I have for each <code>primaryObjectInfo</code>. </p>
  
  <p>Async method <code>objectManager.GetRelatedObjectsAsync()</code> internally call 4-5 async functions and there are functions where parameters are passed by reference. (ref keyword)  </p>
  
  <p><strong>Question:</strong> 
  Am I using the right data structure to consolidate the result from all parallel threads ? 
  If yes, what could be the reason I am getting different result every time?</p>