System.Web.Caching.Cache - Key

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hi, in the System.Web.Caching.Cache class, the Add method takes a
string as key.

The GetEnumerator method returns an IDictionaryEnumerator. On the
returned enumerator there is a Key property. But this property is of
type "object", not "string".

Is that correctly understood?

If so, is it not odd that I can only provide a "string" as a key, yet
when I get the keys back they are "objects"?


Thanks,
Peter
 
If so, is it not odd that I can only provide a "string" as a key, yet
when I get the keys back they are "objects"?

Well, they still *are* strings - it is just typed as object because it
is using the IDictionaryEnumerator interface, largely in part since it
pre-dates generics introduced in .NET 2.0.

Ultimately it is just a cast - it isn't going to hurt, even if it
isn't 100% ideal.

For completeness, note that the generic IDictionary<TKey,TValue>
interface offers IEnumerable<KeyValuePair<TKey,TValue>>, which would
have given you a string key for something like
Dictionary<string,object>.

Marc
 
Back
Top