无法找到类型或命名空间名称keyedcollection

时间:2012-11-20 17:05:07

标签: c# keyedcollection

我是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。'

帮助!

1 个答案:

答案 0 :(得分:4)

TKeyTItemKeyedCollection<TKey, TItem>的类型参数。

由于您分别使用具体类型KeyedCollection<string, GClass2>stringGClass2继承,因此您应该替换占位符类型TKey和{{1}在您的实现中使用这两种类型:

TItem
相关问题