私は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 が見つかりませんでした」というエラーが表示されました。そこで、プレースホルダー型を置き換えました。
現在、コードは
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);
 }
GetKeyForItem が保護されていることをすっかり忘れていました。新しいエラーは、System.Collections.ObjectModel.KeyedCollection.GetKeyForItem(GCl ass2) をオーバーライドするときにアクセス修飾子を変更できないことを示しています。
「GClass1.GetKeyForItem(GClass2)」は、abstract、extern、または partial とマークされていないため、本体を宣言する必要があるという奇妙なエラーも発生します。
アクセス修飾子の問題に対する回避策はありますか?また、誰かが「マークされていないためボディを宣言する」エラーを説明できますか?
ありがとう!