I want to store DateTime values in a data structure(either a HashSet or List).I later want to use that structure for comparison with some other DateTime values.
- I want to know the different in mechanism of how "==" and contains() works.
- Also I want to know the difference in mechanism with respect to HashSet and List types?
- I assume contains () in HashSet would be O(1) vs O(n) in List?
- What is more reliable to compare DateTime objects "==" or contains?
- Is LINQ faster or the same?
Code -
List<DateTime> list1 = new List<DateTime>();
HashSet<DateTime> list2 = new HashSet<DateTime>();
list1.Add(new DateTime(DateTime.Now.Date));
list2.Add(new DateTime(DateTime.Now.Date));
DateTime someOtherDate = DateTime.Now.Date;
if(list1.Contains(someOtherDate))
//thendosomething
VS
if(list2.Contains(someOtherDate))
//thendosomething
AND
foreach(DateTime tmpValue in list1)
{
if(tmpValue == someOtherDate)
//thendosomehting
}
VS
foreach(DateTime tmpValue in list2)
{
if(tmpValue == someOtherDate)
//thendosomehting
}
All of the above VS -
var rows = from e in list1
where e == someOtherDate
select e