title: Is it safe to left task unreferenced
body: <p>Is it safe to left task unreferenced if i know for sure that it doesn't throw an exception? Will GC wait untill a task completes before collects it?</p>
  
  <p>Here is an example of my method that converts an array of the tasks into one task that completes (is canceled or failed) when all tasks completes. My application fails with task unobserved exception (by logging Task.Id at any place where I use tasks I found that the task that left unobserved is the one supplied to this method or at least has the same id). I have no idea why this happens except the garbage collector collects the task returned from Task.Factory.ContinueWhenAll not waiting when it is completed as such it may also collect all my tasks from the array left unreferenced and if there are at least one failed task it will lead task unobserved exception. Sounds crazy but i don't see another explanation to what heppens. So is it possible? </p>
  
  <pre><code>        public static Task ToWhenAllTask(this Task[] tasks, bool cancelIfAnyCanceled = true)
      {
          if (tasks != null &amp;&amp; tasks.Length == 0)
              throw new ArgumentException();
  
          var tcs = new TaskCompletionSource&lt;object&gt;();
  
          Task.Factory.ContinueWhenAll(tasks, ts =&gt; {
              try
              {
                  List&lt;Exception&gt; errors = null;
                  bool canceled = false;
  
                  foreach (Task task in ts)
                  {
                      AggregateException ex = task.Exception;
  
                      if (ex != null)
                      {
                          if (errors == null)
                              errors = new List&lt;Exception&gt;();
  
                          errors.Add(ex.Flatten());
                      }
  
                      if (task.IsCanceled)
                          canceled = true;
                  }
  
                  if (errors != null)
                      tcs.TrySetException(errors);
                  else if (cancelIfAnyCanceled &amp;&amp; canceled)
                      tcs.TrySetCanceled();
                  else
                      tcs.TrySetResult(null);
              }
              catch(Exception ex)
              {
  
                  // there is nothing to fail in this method but just in case
                  tcs.TrySetException(ex);
              }
  
          }, TaskContinuationOptions.ExecuteSynchronously);
  
          return tcs.Task;
      }
  </code></pre>
  
  <p>PS. To be honest I thought that until task is completed TaskScheduller holds a reference to it (and in my case the continuation task also holds a reference to the tasks array). So GC can't collect the continuation task and all the tasks from array until all of them completes.</p>