FicVic FicVic - 7 months ago 51
C# Question

Compare first and second item on the list

Let's say I'm populating a list with numbers. Every time I press a button a number from textbox get's added to the list.

Now, my questions is: how can I compare the latest item added and second latest item added?

Thanks in advance?


List<T> has an indexer property, so why don't you use it?

var last = list.Count > 0 ? list[list.Count - 1] : null;
var secondLast = list.Count > 1 ? list[list.Count - 2] : null;