3

我正在尝试解决编译器警告:

Type parameter 'TKey' has the same name as the type parameter from outer type 'Common.Core.ObservableDictionary<TKey,TValue>' 

这是有问题的代码:

protected class KeyedDictionaryEntryCollection<TKey> : KeyedCollection<TKey, DictionaryEntry> {

        public KeyedDictionaryEntryCollection() {}

        public KeyedDictionaryEntryCollection(IEqualityComparer<TKey> comparer) : base(comparer) {}

        protected override TKey GetKeyForItem(DictionaryEntry entry) {
            return (TKey) entry.Key;
        }
    }

它显示了第一个 TKey 是问题。

我该如何解决这个问题?代码工作得很好,但我正在努力解决所有编译器警告。

4

1 回答 1

10

This is because this is an inner class inside of a generic class. The compiler is warning you that you're using the same name as the outer class specification, which effectively "hides" it. You can remove this by eliminating the specification on the inner class, since it's not needed (unless you want to introduce a new generic type):

class ObservableDictionary<TKey,TValue>
{
     // This class already knows about TKey and TValue since it's an inner class in the "outer" generic class
     protected class KeyedDictionaryEntryCollection : KeyedCollection<TKey, DictionaryEntry> 
     {
        // Your existing code, as is...
     }
}
于 2013-06-07T19:02:07.200 回答