I have class Foo. Foo has a property of public string x.
I would like to instantiate Foo a few times as ONE and TWO, and add those instances to Hashtable Bar with keys 1 and 2 respectively. How do I obtain string x for the particular instance.
I’ve tried something to the like of: Bar[1].x, but the property x is not recognized.
What am I doing wrong?
You should be using
Dictionary<int, Foo>instead ofHashtable.Hashtableis an obsolete class for the days we didn’t have generics. It stores key and values asobjecttype.Dictionary<TKey,TValue>, on the other hand, is a strongly typed generic collection.If you want to use
Hashtablefor some reason (e.g. C# 1.0), you’ll have to cast the object: