I need to define a class with an internal collection object to hold different types of values(such as string, int, float, DateTime, and bool) by a string key. I could use Hashtable since it is not strongly typed collection class, while the Dictionary is used for strongly typed items (I could use object for Dictionary even though).
Here is the case I would like to define my class and its usage:
public class MyBaseClass<D> {
protected Hashtable ht = new Hashtable();
MyClass public SetValue<T>(string key, T value)
{
ht.Add(key, value);
return this;
}
public abstract D GetObject();
}
// Example of using the base class for data as class Data1
public MyClass<Data1> : MyBaseClass<Data1> {
public Data1 GetObject() {
return new Data1 {
Property1 = ht["key1"] as string,
Property2 = Int.Parse(ht["key2"].ToString())
}
}
With above example, I am not sure what is the best collection to hold my data in MyBaseClass? I read some articles about Hashtable. It is an obsolete collection and the performance comparing to Dictionary collection is much slower. Should I use Dictionary or any other collection available in .Net 3.5?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…