Ich möchte eine KeyedCollection verwenden, um eine Klasse gegen einen String-Schlüsselwert zu speichern. Ich habe den folgenden Code:
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
}
}
Kann mir jemand sagen, was ich hier falsch mache? Wo muss ich den Schlüsselwert angeben, damit ich ihn abrufen kann?