In Today's section, we will talk about the Rx library. Basically, Rx is a library for composing asynchronous and event-based programs using observable collections. This is very useful in the case wherein you are pulling data asynchronously from different sources and then manipulating the same and finally printing the result. In these kinds of scenarios, you have to write lots of glue code and of course, these codes will be error-prone. Let's say one of the sources just throws an error, then what will happen?
This way, you really need to do lots of stuff for the things working fine. Hence, Rx is an answer to this kind of situation, which keeps the thing simple yet lightweight. Rx also uses a LINQ query on the observable collections.
But, it would be nice to talk a little about collections before starting Rx. IEnumerables is one of the most widely used Pull Based collection which is synchronous in nature. The following is the sample snippet for the same.
- interface IEnumerable<out T>
- {
- IEnumerator<T> GetEnumerator();
- }
- interface IEnumerator<out T>:IDisposable
- {
- bool moveNext();
- T currennt { get; }
- void Reset();
- }


- //Observables:- Push based
- interface IObservable<out T>
- {
- IDisposable subscribe(IObserver<T> observer);
- }
- interface IObserver<in T>
- {
- void onNext(T value);
- void onError(Exception ex);
- void onCompleted();
- }





- using System;
- using System.Reactive.Linq;
- namespace ReactiveExtensions
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- IObservable<string> obj = Observable.Generate(
- 0, //Sets the initial value like for loop
- _ => true, //Don't stop till i say so, infinite loop
- i => i + 1, //Increment the counter by 1 everytime
- i => new string('#', i), //Append #
- i => TimeSelector(i)); //delegated this to private method which just calculates time
- //Subscribe here
- using (obj.Subscribe(Console.WriteLine))
- {
- Console.WriteLine("Press any key to exit!!!");
- Console.ReadLine();
- }
- }
- //Returns TimeSelector
- private static TimeSpan TimeSelector(int i)
- {
- return TimeSpan.FromSeconds(i);
- }
- }
- }


Joe ThomasPosted Oct 8, 2019, 2:49 PM
Hi Rahul, can you answer this question here, thanks https://social.msdn.microsoft.com/Forums/en-US/f0803daf-5477-4982-9ebd-56088cada9e2/net-core-even-number-detector-with-reactive-system?forum=rx
Sibeesh VenuPosted Oct 6, 2015, 9:46 AM
Nice Share
Ankit BansalPosted Oct 6, 2015, 3:16 AM
nice..
Santhakumar MunuswamyPosted Oct 5, 2015, 11:50 PM
Nice Share
RakeshPosted Oct 5, 2015, 1:12 PM
Good share
Nilesh JadavPosted Oct 5, 2015, 9:38 AM
Good one sir !