Skip to content
Ian Griffiths Ian Griffiths

Rx.NET

In this episode, Ian Griffiths introduces the new DisposeWith method available in Rx.NET version 6.1, released in October 2025.

He discusses the new functionalities, including a new TakeUntil() overload and the ResetExceptionDispatchState operator, with additional videos to cover these features.

Ian explains that DisposeWith is a community contribution by Chris Pullman, designed to facilitate a fluent coding style by simplifying the disposal of multiple subscriptions. He demonstrates the method in a console application, comparing it with the traditional CompositeDisposable approach, and highlights its ease of use for handling observable subscriptions.

Full documentation is available at Introduction to Rx.NET.

  • 00:00 Introduction to Dispose Operator in Rx.NET
  • 00:43 Community Contribution and Background
  • 01:26 Purpose and Design of DisposeWith Method
  • 01:47 Demo: DisposeWith Method in Action
  • 02:11 CompositeDisposable vs DisposeWith
  • 03:22 Fluent Style Development with DisposeWith
  • 04:32 Conclusion and Further Resources

Transcript

I'm going to tell you about a new operator called DisposeWith that we've added to Rx.NET in version 6.1.

In October 2025, we released Rx v6.1, and it's a new minor release because it has new functionality. It has a new DisposeWith() method, which is the subject of this video. It has a TakeUntil() overload that takes a CancellationToken, and there's another video about that, and there'll also be another video about the new ResetExceptionDispatchState operator.

Now, if you've been following Rx for a while, you know we're also working on fixing some packaging options, but that's not going to happen till the next major release: v7.

So what's this DisposeWith method all about?

The first thing I should say is that this is a community contribution. Thanks very much to Chris Pullman for suggesting this and providing the code, being patient while we got it into place. Now Chris, as you may be aware, works on the ReactiveUI Framework. He's a very active contributor to the open source community, a long-term user of Rx with a deep understanding of the framework, and he felt this would be a very useful feature for the way that people tend to work with ReactiveUI. It's a fluent style of API, and people working with ReactiveUI find this way of working very useful. That's why we've decided to incorporate this suggestion into the Rx library.

The basic purpose of this addition is to make it simpler to discard multiple subscriptions with a single action. The way the API has been designed is to support what we call a fluent coding style. The best way to show how this works will be to show a quick demo.

Let's take a look at this new method in action. Right now I've got a very simple console application using Rx that has a couple of observable sources and it subscribes to both of them, lets those subscriptions run, and then when the user presses enter, it tears each of them down individually. This works. You can see they're both running there. When I hit enter, they both shut down, and that's fine. The thing is that I've had to call dispose separately on each one, and some people find that a bit onerous.

For a long time in Rx, we've had this thing called CompositeDisposable, which is essentially a collection of disposables, so we could just do this: add both of those disposables to this composite, and then we only have to call dispose on that composite object and the behavior should be exactly the same. Both my subscriptions are running. I hit enter. Both of them shut down, but this arguably hasn't really saved me anything because I actually got, if anything, slightly more code here.

We could do a little better. We could observe that this is actually a collection, and so maybe I could use the collection syntax to initialize this thing. Now I've said my composite disposable consists of a couple of these things, and now they're both running. I hit enter, they both get disposed.

That's an option, but I have had to rather reshape my code to work around this particular way of initializing it.

The idea with this new DisposeWith extension method is that it fits in a bit better with how you might want to set up your observable subscriptions in the first place. Instead of having to do this, we can just do .DisposeWith(cd), so we just add this on the tail end of whatever our observable sequence for subscription ends up looking like. Now if I run this once more, I hit enter, my single call to the composite disposable disposes both of them.

It's a very simple change. It doesn't really add anything you couldn't do before, but it does enable this, what we call fluent style, development.

You'll notice that it brought in an additional namespace here. This means that if you're not a fan of fluent APIs—and not everyone is—then this won't appear in your namespace. Because this does become available on all disposables, it's only there if you choose to have it there. But if you like this style of development, then this is there for you.

In conclusion, this is a simple way to use the CompositeDisposable class to handle multiple unsubscriptions in a single action using a fluent style of API. If you want to use this, just download System.Reactive 6.1, add the using directive for the System.Reactive.Disposables.Fluent namespace, and then you can just call DisposeWith to attach each subscription to your existing composite disposable. We've documented this on https://introtorx.com if you want more details.

Thanks for listening. My name's Ian Griffiths.