FicVic FicVic - 1 year ago 84
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?

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download