How to remove the last element added into the List?

C#ListArraylist

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

rows.RemoveAt(rows.Count - 1)

Solution 2 - C#

The direct answer to this question is:

if(rows.Any()) //prevent IndexOutOfRangeException for empty list
{
    rows.RemoveAt(rows.Count - 1);
}

However... in the specific case of this question, it makes more sense not to add the row in the first place:

Row row = new Row();
//...      

if (!row.cell[0].Equals("Something"))
{
    rows.Add(row);
}

TBH, I'd go a step further by testing "Something" against user."", and not even instantiating a Row unless the condition is satisfied, but seeing as user."" won't compile, I'll leave that as an exercise for the reader.

Solution 3 - C#

rows.RemoveAt(rows.Count - 1);

Solution 4 - C#

You can use List<T>.RemoveAt method:

rows.RemoveAt(rows.Count -1);

Solution 5 - C#

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 ...

Solution 6 - C#

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

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

or

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPranavView Question on Stackoverflow
Solution 1 - C#Patrick HofmanView Answer on Stackoverflow
Solution 2 - C#spenderView Answer on Stackoverflow
Solution 3 - C#Pradip DaundeView Answer on Stackoverflow
Solution 4 - C#Selman GençView Answer on Stackoverflow
Solution 5 - C#amdevView Answer on Stackoverflow
Solution 6 - C#Liakat HossainView Answer on Stackoverflow