Here is a working solution with HashSet`1
and IEquatable`1
.
If you need to work with List`1
s, you can use Linq as poined out in the answer by Ned Stoyanov.
Code:
class Program {
static void Main(string[] args) {
var first = new HashSet<Item>() {
new Item() { Name = "a" },
new Item() { Name = "b" },
};
var second = new HashSet<Item>() {
new Item() { Name = "b" },
new Item() { Name = "c" },
};
var both = first.Union(second).ToList();
both.ForEach(Console.WriteLine);
Console.ReadKey();
}
}
public class Item : IEquatable<Item> {
public string Name { get; set; }
public bool IsFound { get; set; }
public override bool Equals(object that) {
return that is Item && this.Equals((Item)that);
}
public bool Equals(Item that) {
return this.Name.Equals(that.Name, StringComparison.InvariantCultureIgnoreCase);
}
public override int GetHashCode() {
return Name.GetHashCode();
}
public override string ToString() {
return Name;
}
}
Output:
a
b
c
Note: Due to the implementation of HashSet`1
s, the order of elements is not respected.