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.
source
share