title: Generics and Rx
body: <p>I am trying to convert this program to use generics. However, on the line</p>
  
  <pre><code>foreach (var observer in observerList)
  {                           
     observer.OnNext(observer);
  }
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>Cannot convert from System.IObserver&lt;T&gt; to T
  </code></pre>
  
  <p>Program:</p>
  
  <pre><code>public sealed class EnumerableObservable&lt;T&gt; : IObservable&lt;T&gt;, IDisposable
  {
      private readonly IEnumerable&lt;T&gt; enumerable;
  
      public EnumerableObservable(IEnumerable&lt;T&gt; enumerable)
      {
          this.enumerable = enumerable;
          this.cancellationSource = new CancellationTokenSource();
          this.cancellationToken = cancellationSource.Token;
  
          this.workerTask = Task.Factory.StartNew(() =&gt;
          {
                  foreach (var value in this.enumerable)
                  {
                      //if task cancellation triggers, raise the proper exception
                      //to stop task execution
  
                      cancellationToken.ThrowIfCancellationRequested();
  
                      foreach (var observer in observerList)
                      {                           
                          observer.OnNext(observer);
                      }
                  }
              }, this.cancellationToken);
      }
  
      //the cancellation token source for starting stopping
      //inner observable working thread
      private readonly CancellationTokenSource cancellationSource;
  
      //the cancellation flag
      private readonly CancellationToken cancellationToken;
  
      //the running task that runs the inner running thread
      private readonly Task workerTask;
  
      //the observer list
      private readonly List&lt;IObserver&lt;T&gt;&gt; observerList = new List&lt;IObserver&lt;T&gt;&gt;();
  
      public IDisposable Subscribe(IObserver&lt;T&gt; observer)
      {
          observerList.Add(observer);
          //subscription lifecycle missing
          //for readability purpose
  
          return null;
      }
  
      public void Dispose()
      {
          //trigger task cancellation
          //and wait for acknoledge
  
          if (!cancellationSource.IsCancellationRequested)
          {
              cancellationSource.Cancel();
              while (!workerTask.IsCanceled)
                  Thread.Sleep(100);
          }
  
          cancellationSource.Dispose();
          workerTask.Dispose();
  
          foreach (var observer in observerList)
              observer.OnCompleted();
      }
  }
  
  public sealed class ConsoleStringObserver&lt;T&gt; : IObserver&lt;T&gt;
  {
      public void OnCompleted()
      {
          Console.WriteLine("-&gt; END");
      }
  
      public void OnError(Exception error)
      {
          Console.WriteLine("-&gt; {0}", error.Message);
      }
  
      public void OnNext(T value)
      {
          Console.WriteLine("-&gt; {0}", value.ToString());
      }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          //we create a variable containing the enumerable
          //this does not trigger item retrieval
          //so the enumerator does not begin flowing datas
  
          var enumerable = EnumerateValuesFromSomewhere();
          using (var observable = new EnumerableObservable&lt;string&gt;(enumerable))
          using (var observer = observable.Subscribe(new ConsoleStringObserver&lt;string&gt;()))
          {
              //wait for 2 seconds than exit
              Thread.Sleep(2000);
          }
  
          Console.WriteLine("Press RETURN to EXIT");
          Console.ReadLine();
      }
  
      static IEnumerable&lt;string&gt; EnumerateValuesFromSomewhere()
      {
          var random = new Random(DateTime.Now.GetHashCode());
  
          while (true) //forever
          {
              //returns a random integer number as string
              yield return random.Next().ToString();
  
              //some throttling time
              Thread.Sleep(100);
          }
      }
  }
  </code></pre>