AstronBnX
AstronBnX

Reputation: 3

C# Type casting or convertion

public class ExpiringDictionary<TKey, TValue> : IDictionary<TKey, TValue> {
    private class ExpiringValueHolder<T>
    {
        public T Value { get; set; }
        .
        .
        .
    }
    private IDictionary<TKey, ExpiringValueHolder<TValue>> innerDictionary;

    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    {
        //throw new NotImplementedException();
        return (IEnumerator<KeyValuePair<TKey, TValue>>)innerDictionary.GetEnumerator();
    } 
}

Here is code gives me casting error. Is that possible to cast return value for the function GetEnumerator() ?

Thank you for helping.

Upvotes: 0

Views: 65

Answers (1)

Lukazoid
Lukazoid

Reputation: 19426

Your innerDictionary contains a collection of KeyValuePair<TKey, ExpiringValueholder<TValue>>, so not it is not possible in the way you are currently trying.

The easiest solution may be to do the following:

return innerDictionary.Select(kvp => new KeyValuePair<TKey, TValue>(kvp.Key, kvp.Value.Value))
    .GetEnumerator()

This will select the inner Value from your ExpiringValueHolder<T>.

Upvotes: 1

Related Questions