2

I have started researching Reactive extensions and I would like to know how to do the following (ill try and keep it simple):

  1. Have a list of string (or any other type)

  2. When an item is added to the said list, do something with it.

Stuart Blackler
  • 3,732
  • 5
  • 35
  • 60

3 Answers3

9

You can't do that with the existing List<String> class - it doesn't provide notifications, and there's nothing that Reactive Extensions can do about that.

You might want to look at ObservableCollection<T> though.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
6

Get ReactiveUI, then you can use the ReactiveCollection class - then you can use the ItemsAdded Observable.

ReactiveCollection<int> someCollection;

someCollection.ItemsAdded
    .Where(x => x > 100)
    .Subscribe(x => Console.WriteLine("Whoa! A big item was added!"));
Ana Betts
  • 73,868
  • 16
  • 141
  • 209
1

If you have a List<T>, or really any collection that implements IEnumerable then Rx isn't the answer. But if you have a sequence of items, some of which don't exist yet, then by all means, Rx is a good solution.

Scott Weinstein
  • 18,890
  • 14
  • 78
  • 115