How to make a object delete itself from a list. [c

2020-05-10 08:34发布

So I have some objects in a list. I want to make my object to have a method that when called will delete itself from the list. How could I do that?

1条回答
唯我独甜
2楼-- · 2020-05-10 08:54

Is this a trick question?

public class MyObject
{
    public void RemoveFromList(List<MyObject> list)
    {
        if (list == null)
            return;

        list.Remove(this);
    }
}
查看更多
登录 后发表回答