Collection of unique ids
-
I have used
list<>
data type to store emp id. As
List<int> EmpIds = new List<int>() ;
But it takes duplicate entries. Which data structure I should use to have collection of unique ids.
Have a look at
HashSet<T>
; it requires .NET 3.5+ :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
-
I have used
list<>
data type to store emp id. As
List<int> EmpIds = new List<int>() ;
But it takes duplicate entries. Which data structure I should use to have collection of unique ids.
System.Collections.Generic.HashSet
-
I have used
list<>
data type to store emp id. As
List<int> EmpIds = new List<int>() ;
But it takes duplicate entries. Which data structure I should use to have collection of unique ids.
One slight caveat with HashSet: the data in a HashSet is not sorted. You cannot guarantee what order the entries will be in when you enumerate over them. If that doesn't matter to you, HashSet is great. If the order matters, then you could use SortedSet (which would sort them in numeric sequence). If you need them in some other order (e.g. the order in which they were added) you may be better off building your own UniqueList (which isn't hard to do, just an IList wrapper around List to check if the entry is already there when you add it).