Реализуйте IEnumrable <T>для словаря с собственным KeyValuePair - PullRequest
0 голосов
/ 26 сентября 2018

После моего вопроса Проблема с реализацией собственной версии KeyValuePair Теперь я хочу изменить код класса

   public class VectorType<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
    {
        string variableName;
        Dictionary<TKey, TValue> paramteresList;

        public VectorType(string variableName)
        {
            this.variableName = variableName;
            paramteresList = new Dictionary<TKey, TValue>();
        }

        public void Add(TKey parameterName, TValue parametereValue)
        {
            paramteresList.Add(parameterName, parametereValue);
        }

        public int NumberOfParameters
        {
            get { return paramteresList.Count; }
        }

        public string VariableName
        {
            get { return variableName; }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)paramteresList).GetEnumerator();
        }

        IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
        {
            return paramteresList.GetEnumerator();
        }
    }

, чтобы использовать собственную реализацию KeyValuePair как

public struct ParameterNameKeyValuePair<TParameterNameKey, TValue>
{
    private TParameterNameKey key;
    private TValue value;

    public ParameterNameKeyValuePair(TParameterNameKey key, TValue value)
    {
        this.key = key;
        this.value = value;
    }

    public TParameterNameKey Key
    {
        get { return key; }
    }

    public TValue Value
    {
        get { return value; }
    }
}

Если я изменю KeyValuePair на ParameterNameKeyValuePair, то у меня будет

    public class VectorType<TParameterNameKey, TValue> : IEnumerable<ParameterNameKeyValuePair<TParameterNameKey, TValue>>
    {
        string variableName;
        Dictionary<TParameterNameKey, TValue> paramteresList;

        public VectorType(string variableName)
        {
            this.variableName = variableName;
            paramteresList = new Dictionary<TParameterNameKey, TValue>();
        }

        public void Add(TParameterNameKey parameterName, TValue parametereValue)
        {
            paramteresList.Add(parameterName, parametereValue);
        }

        public int NumberOfParameters
        {
            get { return paramteresList.Count; }
        }

        public string VariableName
        {
            get { return variableName; }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)paramteresList).GetEnumerator();
        }

        IEnumerator<ParameterNameKeyValuePair<TParameterNameKey, TValue>> IEnumerable<ParameterNameKeyValuePair<TParameterNameKey, TValue>>.GetEnumerator()
        {
            return paramteresList.GetEnumerator();
        }
    }

и у меня есть ошибка во время компиляции: невозможно неявно преобразовать тип 'System.Collections.Generic.Dictionary.Enumerator' в 'System.Collections.Generic.IEnumerator>'
в строке

        IEnumerator<ParameterNameKeyValuePair<TParameterNameKey, TValue>> IEnumerable<ParameterNameKeyValuePair<TParameterNameKey, TValue>>.GetEnumerator()
        {
            return paramteresList.GetEnumerator();
        }

Я изменяю только KeyValuePair на ParameterNameKeyValuePair.Почему у меня есть эта ошибка?И второй вопрос: безопасно ли реализовывать собственную KeyValuePair, как в моем случае ParameterNameKeyValuePair?

...