-1

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?

vroomvsr
  • 93
  • 2
  • 8
  • In order for that to happen, the object would have to have a reference to the list or else it would have to raise an event that the list could handle. Without one of those two things, it would require magic. – jmcilhinney Apr 20 '15 at 02:03
  • Your object have to have a reference to the list and when some call the method you said, you have to call the `remove` method from the list with `this` as parameter. – CrApHeR Apr 20 '15 at 02:04
  • 2
    It's not very clear what you're trying to do. You should explain your problem in more detail and show what you tried to solve it so far. – xxbbcc Apr 20 '15 at 02:04
  • 1
    http://xyproblem.info/ – jdphenix Apr 20 '15 at 02:09

1 Answers1

9

Is this a trick question?

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

        list.Remove(this);
    }
}
Mark E
  • 721
  • 6
  • 16