KeyedCollectionを使用して、文字列キー値に対してクラスを格納したいと思います。私は次のコードを持っています:
public class MyClass
{
public string Key;
public string Test;
}
public class MyCollection : KeyedCollection<string, MyClass>
{
public MyCollection() : base()
{
}
protected override String GetKeyForItem(MyClass cls)
{
return cls.Key;
}
}
class Program
{
static void Main(string[] args)
{
MyCollection col = new MyCollection();
col.Add(new MyClass()); // Here is want to specify the string Key Value
}
}
誰かが私がここで間違っていることを教えてもらえますか?キー値を取得できるように、どこでキー値を指定しますか?