I\'m looking to convert an IEnumerable collection to an IObservable one without using Rx ToObservable() and ToEnumerable() methods.
Simple answer - use ToObservable
. That's what it's for.
"Answering the actual question" answer - you can avoid using Subjects
via Observable.Create
:
void Main()
{
var src = Enumerable.Range(0, 10);
var observable = Observable.Create(obs =>
{
foreach(var item in src)
{
obs.OnNext(item);
}
return Disposable.Create(()=>{});
});
using(observable.Subscribe(Console.WriteLine))
{
Console.ReadLine();
}
}
Output:
0
1
2
3
4
5
6
7
8
9