I'm attempting to create an IObservable<T>
from two arrays (IEnumerable
s). I'm trying to avoid explicitly iterating over the arrays and calling observer.OnNext
. I came across the Observable.Subscribe extension method, which at first glance would appear to be what I need. However, it does not work as I expected to and I'm at a loss as to why.
The following code is an example:
class Program
{
static void Main(string[] args)
{
var observable = Observable.Create<char>(observer =>
{
var firstBytes = new[] {'A'};
var secondBytes = new[] {'Z', 'Y'};
firstBytes.Subscribe(observer);
secondBytes.Subscribe(observer);
return Disposable.Empty;
}
);
observable.Subscribe(b => Console.Write(b));
}
}
The output of this is "AZ", not "AZY" as I expected. Now, if I subscribe to secondBytes
before firstBytes
, the output is "ZAY"! This seems to suggest it is enumerating the two arrays in-step - which kind of explains the "AZ" output.
Anyhow, I'm at a complete loss as to why it behaves like this and would appreciate any insight people may be able to provide.