gpt4 book ai didi

c# - 如何解决警告 : TKey has the same name as the type parameter from outer type

转载 作者:行者123 更新时间:2023-11-30 13:47:32 26 4
gpt4 key购买 nike

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

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 是问题。

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

最佳答案

这是因为这是泛型类内部的内部类。编译器警告您,您正在使用与外部类规范相同的名称,这有效地“隐藏”了它。您可以通过消除内部类的规范来删除它,因为不需要它(除非您想引入新的泛型类型):

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

关于c# - 如何解决警告 : TKey has the same name as the type parameter from outer type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16991092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com