Subscribe and immediately unsubscribe after first action
问题 I want to subscribe on an IObservable<T> and unsubscribe (dipose) the subscription right after receiving the first element of type T , i.e. I only want to call the action on the very first element I get after subscribing. This is the approach I came up with: public static class Extensions { public static void SubscribeOnce<T>(this IObservable<T> observable, Action<T> action) { IDisposable subscription = null; subscription = observable.Subscribe(t => { action(t); subscription.Dispose(); }); }