What is the best way to make a data structure similar to HashSet but unique types instead of values.
How can I simplify this code, it seems to me that I'm doing something wrong and going through the whole list is unnecessary.
An example of what I have:
public abstract class Foo {}
public class FooA : Foo {}
public class FooB : Foo {}
public class FooList : List<Foo>
{
public new void Add(Foo fooItem)
{
// как то без перебора
foreach (var item in this)
{
if (item.GetType() == fooItem.GetType())
return;
}
base.Add(fooItem);
}
}
This is how it is used:
FooA fooA = new FooA();
FooB fooB = new FooB();
FooB anotherFooB = new FooB();
FooList list = new FooList();
list.Add(fooA);
list.Add(fooB);
list.Add(anotherFooB);
foreach(var item in list)
{
Console.WriteLine(item);
}
/* Output:
FooA
FooB
*/