title: Removing items from list in an async function
body: <p>I'm writing a function to remove all subscriptions by keeping a list of the subscription IDs in a List.</p>
  
  <p>I was having issues with </p>
  
  <pre><code>     foreach (var subscriptionId in _listOfSubscriptionIds)
       {
             await _hubProxy.Invoke("removeSubscription", subscriptionId);
             _listOfSubscriptionIds.Remove(subscriptionId);
       }
  </code></pre>
  
  <p>because modifying the list in a foreach loop was throwing errors. I found out on this community that I could do <code>foreach(var subscriptions in _listOfSubscriptionIds.ToList())</code> but that sounded bad in terms of space complexity, especially if there were around 10000000 subscriptions. </p>
  
  <p>So I changed it to the following: </p>
  
  <pre><code>    public async Task RemoveSubscription (string subscriptionId)
      {
          await _hubProxy.Invoke("removeSubscription", subscriptionId);
          _listOfSubscriptionIds.Remove(subscriptionId);
      }
      public void RemoveAllSubscriptions()
      {
          _listOfSubscriptionIds.ForEach(async subscriptionId =&gt; await RemoveSubscription(subscriptionId));
      }
  </code></pre>
  
  <p>Some questions:</p>
  
  <ol>
  <li><p>I think the second one is better in terms of space and time complexity, but am I right? </p></li>
  <li><p>Is there is a possible race-condition in the second case that I may be missing?</p></li>
  <li><p>Within RemoveSubscription, is <code>_listOfSubscriptionIds.Remove(subscriptionId)</code> also waited upon because it is in an async function...?</p></li>
  </ol>
  
  <p>Edit: A detail that I didn't share that makes calling <code>_listOfSubscriptionIds.Clear()</code> or iterating through the <code>_listOfSubscriptionIds</code> problematic is that there may be multiple clients on the <code>_hubProxy</code>, each with their unique set of subscriptions.</p>
  
  <p>client 1 with subscriptionIds {a, b, c, d, e}</p>
  
  <p>client 2 with subIds {f, g, h, I, j, k}</p>
  
  <p>client 3 with subIds, {l, m, n, o, p}, etc. </p>
  
  <p>which means when I call client1.RemoveAllSubscriptions(), it may wipe out client2, client3, etc.'s subscriptionIds as well.</p>