How to remove the last element added into the List?

前端 未结 6 1175
耶瑟儿~
耶瑟儿~ 2020-12-18 17:40

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

6条回答
  •  囚心锁ツ
    2020-12-18 18:17

    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 myList) {
        myList.RemoveAt(myList.Count - 1);
    }
    

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

    public string pop (List 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 ...

提交回复
热议问题