我是C#的新手,所以如果这是一个愚蠢的问题请原谅我。我遇到了错误,但我不知道如何解决它。我正在使用Visual Studio 2010。
此代码行
public class GClass1 : KeyedCollection<string, GClass2>
给我错误
'GClass1' does not implement inherited abstract member 'System.Collections.ObjectModel.KeyedCollection<string,GClass2>.GetKeyForItem(GClass2)'
从我读过的内容可以通过在继承的类中实现抽象成员来解决这个问题
public class GClass1 : KeyedCollection<string, GClass2>
{
public override TKey GetKeyForItem(TItem item);
protected override void InsertItem(int index, TItem item)
{
TKey keyForItem = this.GetKeyForItem(item);
if (keyForItem != null)
{
this.AddKey(keyForItem, item);
}
base.InsertItem(index, item);
}
然而,这让我错误地说'无法找到类型或命名空间名称找不到TKey / TItem。'
帮助!
答案 0 :(得分:4)
TKey
和TItem
是KeyedCollection<TKey, TItem>
的类型参数。
由于您分别使用具体类型KeyedCollection<string, GClass2>
和string
从GClass2
继承,因此您应该替换占位符类型TKey
和{{1}在您的实现中使用这两种类型:
TItem