this is an easy one. I have a List<T> in C# and I woudl like to add some elements on a LIFO basis, so on the "bottom" of the List<T>.
For several reasons I cannot use the Stack class.
Thanks
Francesco
Yes the Add() method adds to the end of the list, you can use RemoveAt(yourList.Count - 1) to remove last, and yourList[yourList.Count - 1] to peek at the last.
Though I'm curious, why can't you use the Stack() class?
Items added to a List<T> using the Add method are placed at the end of the list. If you want to process the list so that it's LIFO, either iterate in reverse (meaning you change the way you process the list) or always use Insert(0, item) to add elements to the list (meaning you change the way you populate the list).
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