我是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。’
帮助!
发布于 2012-11-21 01:07:00
TKey和TItem是KeyedCollection<TKey, TItem>的类型参数。
由于继承自KeyedCollection<string, GClass2>的具体类型分别为string和GClass2,因此应将实现中的占位符类型TKey和TItem替换为这两种类型:
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);
}https://stackoverflow.com/questions/13478314
复制相似问题