I’m still new to C#. I want to have a List<ElementObj> in a C# application that I am writing. The List has to maintain a set of unique elements, ie, no duplicated elements in the List. I also intend to sort the list’s order according to the type attribute of ElementObj.
I intend to use the List data structure from System.Collections.Generics. But List don’t maintain uniqueness on its own. So, every time before I add an element to the list, I may have to loop through the entire list to check if an element already exists in the List. This doesn’t sound very efficient.
I read that I could use a HashSet. This will ensure uniqueness of the elements in the set. But, the problem is I cannot access the elements in HashSet through an index, say myHashSet[0].
In a situation like this, what would be the best data structure or effective solution to it?
I would create my own collection class which keeps the objects internally in both a hashset and a list.