Custom System.Collections.Generic.Contains to test custom object
How can I rewrite or is there a way to write my own custom function that mimics custom System.Collections.Generic.Contains but only factors in some common properties of the custom object?
For example, if I have a custom object with a property name and ID, I would like my list of unique values ββto contain all DISTINCT names. The identifier is irrelevant in this case.
List allvalues = new List ({0, "Burger"}, {1, "Pizza"}, {2, "burger"})
I would like it to return me a List containing the first object 0, Burger and 1, Pizza ... Regardless of the id and case of the name.
Yes, use refund refund as in:
private Collection<T> internalCollection;
public Collection<T> GetDistinctList<T>()
{
List<string> names = new List<string>();
foreach(T thisT in internalCollection)
if (!names.Contains(thisT.Name)
{
names.Add(thisT.Name);
yield return thisT;
}
}
I don't quite understand how Contains(...)
related to the material List ({0, "Burger"}, {1, "Pizza"}, {2, "burger"})
. Can you clarify? In general, you can see how to create a custom Equals()
/ GetHashCode()
or IEquatable<T>
or IEqualityComparer<T>
. Also, LINQ has many ways to make it simple - do you have access to LINQ?