首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到类型或命名空间名称keyedcollection

找不到类型或命名空间名称keyedcollection
EN

Stack Overflow用户
提问于 2012-11-21 01:05:07
回答 1查看 606关注 0票数 2

我是C#的新手,所以如果这是一个愚蠢的问题,请原谅我。我遇到一个错误,但我不知道如何解决它。我使用的是Visual Studio 2010。

此代码行

代码语言:javascript
复制
public class GClass1 : KeyedCollection<string, GClass2>

给了我这个错误

代码语言:javascript
复制
'GClass1' does not implement inherited abstract member 'System.Collections.ObjectModel.KeyedCollection<string,GClass2>.GetKeyForItem(GClass2)'

据我所知,这个问题可以通过在继承类中实现抽象成员来解决,如下所示

代码语言:javascript
复制
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。’

帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-21 01:07:00

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

由于继承自KeyedCollection<string, GClass2>的具体类型分别为stringGClass2,因此应将实现中的占位符类型TKeyTItem替换为这两种类型:

代码语言:javascript
复制
public class GClass1 : KeyedCollection<string, GClass2>
{
  public override string GetKeyForItem(GClass2 item);
  protected override void InsertItem(int index, GClass2 item)
  {
    string keyForItem = this.GetKeyForItem(item);
    if (keyForItem != null)
    {
        this.AddKey(keyForItem, item);
    }
    base.InsertItem(index, item);
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13478314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档