I have a List in c# in which i am adding list fields.Now while adding i have to check condition,if the condition satisfies then i need to remove the last row added from the list. Here is my sample code..
List<> rows = new List<>(); foreach (User user in users) { try { Row row = new Row(); row.cell = new string[11]; row.cell[1] = user.""; row.cell[0] = user.""; row.cell[2] = user.""; rows.Add(row); if (row.cell[0].Equals("Something")) { //here i have to write code to remove last row from the list //row means all the last three fields } }
So my question is how to remove last row from list in c#. Please help me.
The simplest approach is to use the list's pop([i]) function, which removes an element present at the specified position in the list. If we don't specify any index, pop() removes and returns the last element in the list.
We can use the remove() method of ArrayList container in Java to remove the last element. ArrayList provides two overloaded remove() method: remove(int index) : Accept index of the object to be removed. We can pass the last elements index to the remove() method to delete the last element.
I think the most efficient way to do this is this is using RemoveAt
:
rows.RemoveAt(rows.Count - 1)
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