So the following code will do a dump of the whole list every second.
var list = new List<object>();
for (int i = 0; i < 100; i++)
{
list.Add(new { A = i.ToString(), B = new Random().Next() });
list.Dump(); // How to DumpLatest()?
Thread.Sleep(1000);
}
But how can I make it to just update the dump output without adding a new one?
There is a related Q/A here but it doesn't work for me.
The DumpLatest()
extension method only applies to IObservable<T>
; there's no way to detect that an item is added to a List<T>
, so LinqPad can't display the last value added.
Instead you can use a DumpContainer
and change its content explicitly:
var list = new List<object>();
var container = new DumpContainer();
container.Dump();
for (int i = 0; i < 100; i++)
{
var item = new { A = i.ToString(), B = new Random().Next() };
list.Add(item);
container.Content = item;
Thread.Sleep(1000);
}
You could also achieve the same result with a Subject<T>
(arguably more elegant):
var subject = new Subject<object>();
subject.DumpLatest();
for (int i = 0; i < 100; i++)
{
var item = new { A = i.ToString(), B = new Random().Next() };
subject.OnNext(item);
Thread.Sleep(1000);
}
EDIT: OK, I thought you wanted to see only the last item. To print the whole list, just use subject.Dump()
, as mentioned by Joe in the comments. If you use the first approach, put the list itself in the DumpContainer
, and call Refresh()
on it in the loop.
Basically same with Thomas Levesque's answer, a little shorter.
Observable.Interval(TimeSpan.FromSeconds(1))
.Select(t=> new { A = t.ToString(), B = new Random().Next() })
.Take(100)
.Dump(); // all 100
//.DumpLatest(); //only latest one
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With