I want to have a time and value class, where the value might be any type, and then put that into some kind of collection.
public class TimeValue<T> {
public DateTime TimeStamp { get; set; } = DateTime.Now;
public T Value { get; set; } = default(T);
TimeValue() { } // constructors
TimeValue(DateTime ts, T val) { TimeStamp = ts; Value = val; }
TimeValue(TimeValue<T> tv) { TimeStamp = tv.TimeStamp; Value = tv.Value; }
}
This oldish answer Collection of generic types referred to by Different Generics T in the same collection requires putting Type within the class and use of a Dictionary
and I don't want to mix types within the collection as the referring question does.
I'd like to specify collection the types (as below), which only specifies the type for the Value. Then the collection will be of essentially TimeValue< typeof(Value)> entries:
TimeValueCollection<double> timeDoubles;
TimeValueCollection<int> timeInts;
After Jonny's comment, this is what I have. Not sure how IEquatable<TimeValue<T>>
would work or which collection will suit the need best.
public class TimeValueCollection<T> : ICollection<TimeValue<T>> {
ICollection<TimeValue<T>> items;
public TimeValueCollection() => items = new List<TimeValue<T>>();
// todo:
// properties matching collection chosen.
// methods matching collection chosen
}
With Jonny's second comment, I can't see any benefit in the construct I was pursuing over this:
public class TimeValueCollection<DateTime,T> : IDictionary<DateTime,T>
{
IDictionary<DateTime, T> items;
public TimeValueCollection() => items = new Dictionary<DateTime, T>();
private Type typeName = typeof(T);
public Type ValueType { get => typeName; }
public T this[DateTime key] { get => items[key]; set => items[key] = value; }
// todo: remaining properties and methods...
}