[c#] How to remove the last element added into the List?

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.

This question is related to c# list arraylist

The answer is


I think the most efficient way to do this is this is using RemoveAt:

rows.RemoveAt(rows.Count - 1)

if you need to do it more often , you can even create your own method for pop the last element; something like this:

public void pop(List<string> myList) {
    myList.RemoveAt(myList.Count - 1);
}

or even instead of void you can return the value like:

public string pop (List<string> myList) {
    // first assign the  last value to a seperate string 
    string extractedString = myList(myList.Count - 1);
    // then remove it from list
    myList.RemoveAt(myList.Count - 1);
    // then return the value 
    return extractedString;
}

just notice that the second method's return type is not void , it is string b/c we want that function to return us a string ...


I would rather use Last() from LINQ to do it.

rows = rows.Remove(rows.Last());

or

rows = rows.Remove(rows.LastOrDefault());

You can use List<T>.RemoveAt method:

rows.RemoveAt(rows.Count -1);

rows.RemoveAt(rows.Count - 1);

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to list

Convert List to Pandas Dataframe Column Python find elements in one list that are not in the other Sorting a list with stream.sorted() in Java Python Loop: List Index Out of Range How to combine two lists in R How do I multiply each element in a list by a number? Save a list to a .txt file The most efficient way to remove first N elements in a list? TypeError: list indices must be integers or slices, not str Parse JSON String into List<string>

Examples related to arraylist

Adding null values to arraylist How to iterate through an ArrayList of Objects of ArrayList of Objects? Dynamically adding elements to ArrayList in Groovy How to replace existing value of ArrayList element in Java How to remove the last element added into the List? How to append elements at the end of ArrayList in Java? Removing Duplicate Values from ArrayList How to declare an ArrayList with values? In Java, can you modify a List while iterating through it? Load arrayList data into JTable