Seems to me your best bet is just a List<T> that you protect with a ReaderWriterLockSlim. For example:
class Sample
{
public DateTime EventTime { get; set; }
public decimal Value { get; set; }
}
List<Sample> _samples = new List<Sample>();
ReaderWriterLockSlim _samplesLock = new ReaderWriterLockSlim();
// to get items after a particular date
List<Sample> GetSamplesAfterDate(DateTime dt)
{
_samplesLock.EnterReadLock();
try
{
return _samples.Where(s => s.EventTime >= dt).ToList();
}
finally
{
_samplesLock.ExitReadLock();
}
}
If your list is known to be in chronological order, then you can improve the performance by using binary search on the list to find the first item that's greater than or equal to your passed time stamp. I just used the LINQ version here because the point is to illustrate the locking.
Appending to the list is similar: acquire the write lock, append, and release the lock:
void AppendSample(Sample s)
{
_samplesLock.EnterWriteLock();
try
{
_samples.Add(s);
}
finally
{
_samplesLock.ExitWriteLock();
}
}
An alternative is to use List<KeyValuePair<DateTime, decimal>>
rather than List<Sample>
. The locking would remain the same.
This should perform quite well in most situations.