how to remove an element from a list of user define class?
Solution 1
This will remove all Level2
objects with Price = 300 and Volume = 400
from the list
ask.RemoveAll(a => a.price == 300 && a.volume == 400);
Solution 2
Here's how I'd do it: override Equals
and GetHashCode
on Level2
, so that you can use List<T>.Remove
without needing the 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, which works off of the identity of the object instead of the values inside of 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 these, or one of the other answers, may be the best approach.
Solution 3
Remember that you are adding instantiated objects
to the typed List bid, so you'll need to search your list for objects to remove that match a certain condition. Note that you could have multiple (different) instances of Level2
in your list with price==300
and volume==400
.
A small code sample should clarify things.
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());
}
}
Clayton Leung
Updated on June 20, 2022Comments
-
Clayton Leung almost 2 years
I am fairly new to c#, please be gentle to me, I have been searching through the net for a few hours without success, I want to remove an element from my user defined class. How do I do it?
below is the snippet of the 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 }
I think I need to implement IEnumerable of some sort, but how does the syntax look like? can someone please give me a working snippet? thanks a lot