How do I reset the last list in LinqPad?

So the following code will reset the entire 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 get 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.

+3


source to share


2 answers


The extension method DumpLatest()

only applies to IObservable<T>

; there is no way to detect that an item has been added to List<T>

, so LinqPad cannot display the last added value.

Instead, you can use DumpContainer

and explicitly change its content:

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 can also achieve the same result with Subject<T>

(possibly 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 only wanted to see the last item. To print the entire list, just use subject.Dump()

as Joe mentioned in the comments. If you are using the first approach, put the list itself in DumpContainer

and call Refresh()

on it in a loop.

+5


source


Basically the 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

      

+1


source







All Articles