Dictionary using the Value property as a key

At the end of 2008, I used the BCL dictionary, which set the key based on the obect value property that it stored. Now I can not find this dictionary. Can someone remind me? Here is what I recall about this:

  • It requires a class that was used as <value> to implement an interface that had a method or property that determined which field / member should be considered the key.
  • It was a dictionary that is used or defined in one of the ServiceModel namespaces.

I tried using the reflector tool to find all the dictionary classes in BCL, and I did not notice it. Perhaps the word "Dictionary" was not in the name of this magic class that I once used.

+4
source share
1 answer

Perhaps you remember an abstract class KeyedCollection<,>? He set the key based on everything you want from the subject.

public class MyObject
{
    public string Key
    {
        get;
        set;
    }

    public int Foo
    {
        get;
        set;
    }
}

public class MyObjectCollection : KeyedCollection<string, MyObject>
{
    protected override string GetKeyForItem(MyObject item)
    {
        return item.Key;
    }
}

In practice, I find LINQ ToDictionary()more useful.

http://msdn.microsoft.com/en-us/library/ms132438(v=vs.110).aspx

+5
source

Source: https://habr.com/ru/post/1532934/


All Articles