如何删除添加到列表中的最后一个元素? [英] How to remove the last element added into the List?

查看:171
本文介绍了如何删除添加到列表中的最后一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中我加入列表fields.Now同时加入我要检查的条件,如果条件满足,然后我需要从列表中删除添加的最后一行C#的List。
下面是我的示例code ..

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

            }

        }

所以我的问题是如何从名单在C#中删除最后一行。
请帮我。

So my question is how to remove last row from list in c#. Please help me.

推荐答案

哦,亲爱的。那岂不是更有意义不添加行的第一个地方?

Oh dear. Wouldn't it make 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,我想进一步通过测试东西用户走了一步。,和甚至没有实例化除非条件得到满足,但看到用户。将无法编译,我想留,作为一个读者练习。

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.

这篇关于如何删除添加到列表中的最后一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆