gpt4 book ai didi

c# - 找不到类型或命名空间名称 keyedcollection

转载 作者:行者123 更新时间:2023-11-30 21:02:29 25 4
gpt4 key购买 nike

我是 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 找不到。”

帮助!

最佳答案

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

因为您继承自 KeyedCollection<string, GClass2>与具体类型 stringGClass2分别,你应该替换占位符类型 TKeyTItem在您使用这两种类型的实现中:

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

关于c# - 找不到类型或命名空间名称 keyedcollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13478314/

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