Greg Gum
Greg Gum

Reputation: 38119

How to resolve warning: TKey has the same name as the type parameter from outer type

I am trying to resolve a compiler warning:

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

This is the code in question:

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;
        }
    }

It shows the first TKey to be the issue.

How do I resolve this? The code works just fine, but I am working to resolve all compiler warnings.

Upvotes: 3

Views: 813

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564851

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...
     }
}

Upvotes: 10

Related Questions