How to remove an item from a custom class list?

I am new to C #, please be considerate of me, I have been searching for a network for several hours without success, I want to remove an element from my user-defined class. How to do it?

below is a snippet of code.

public class Level2
{
    public double price { get; set; }
    public long volume { get; set; }

    public Level2(double price, long volume)
    {
        this.price = price;
        this.volume = volume;
    }
}

static void Main()
{

    List<Level2> bid = new List<Level2>();

    ask.Add(new Level2(200, 500));
    ask.Add(new Level2(300, 400));
    ask.Add(new Level2(300, 600));


    // how to remove this element ???
    ask.Remove(300, 400);       //doesn't work

 }

It seems to me that I need to somehow implement IEnumerable, but what does the syntax look like? can someone please give me a working snippet? many thanks

+5
source share
4 answers

This will remove all Level2objects from the list. Price = 300 and Volume = 400

ask.RemoveAll(a => a.price == 300 && a.volume == 400);
+9
source

, objects , , . , () Level2 price==300 volume==400.

.

public class Level2
{
    public double price { get; set; }
    public long volume { get; set; }
    public Level2(double price, long volume)
    {
        this.price = price;
        this.volume = volume;
    }
    public override string ToString()
    {
        return String.Format("Level 2 object with price: {0} and volume: {1}", 
            this.price, this.volume);
    }
}

public static void Main()
{
    List<Level2> bid = new List<Level2>();
    bid.Add(new Level2(200, 500));
    bid.Add(new Level2(300, 400));
    bid.Add(new Level2(300, 600));
    bid.Add(new Level2(300, 400)); // second item with these quantities

    List<Level2> toRemove = 
        bid.Where(x => x.price == 300 && x.volume == 400).ToList<Level2>();

    foreach (Level2 item in toRemove)
    {
        bid.Remove(item);
        Console.WriteLine("removing " + item.ToString());
    }
}
+1

Here's how I would do it: override Equalsand GetHashCodeon Level2so that you can use List<T>.Removewithout requiring an original reference to the object.

public class Level2
{
    public double price { get; set; }
    public long volume { get; set; }

    public Level2(double price, long volume)
    {
        this.price = price;
        this.volume = volume;
    }

    public override bool Equals(object obj)
    {
        var other = obj as Level2;
        if (other == null)
            return false;
        return other.price == this.price && other.volume == this.volume;
    }

    public override int GetHashCode()
    {
        return price.GetHashCode() ^ volume.GetHashCode();
    }
}

static void Main(string[] args)
{
    List<Level2> ask = new List<Level2>();

    ask.Add(new Level2(200, 500));
    ask.Add(new Level2(300, 400));
    ask.Add(new Level2(300, 600));

    ask.Remove(new Level2(300, 400));
}

Here's another way that works from the identity of the object instead of the values ​​inside it:

static void Main(string[] args)
{
    List<Level2> ask = new List<Level2>();

    ask.Add(new Level2(200, 500));
    var level = new Level2(300, 400);
    ask.Add(level);
    ask.Add(new Level2(300, 600));

    ask.Remove(level);
}

Depending on your situation, one of them or one of the other answers may be the best.

+1
source

First you need to extract the item you want to remove:

Level2 element = ask.FirstOrDefault(l => l.price == 300 && l.volume == 400);
if (element != null)
    ask.Remove(element);
0
source

All Articles