How do I remove the last two rows from a List.
At present, I am removing the 2 rows manually as shown below. However, there might be an instance where I have to define the number of rows to be removed as
n. So, it will remove
n number of rows from the bottom of the list. How can I make this change?
rows.RemoveAt(rows.Count - 1); rows.RemoveAt(rows.Count - 2);
As Spender said, there is a potential issue in your code. I call this a "Index Race Condition". After you remove the last element, something else becomes the new last Element. It is something you have to look out for with Indexed Collections. Keyd collections do not suffer from it.
In addition to what others said regarding using "RemoveRange" (if availible) or LINQ, there is also a chance you got the wrong collection type.