dictionary - .NET class for quickly find out Value based on Key and vice versa -
dictionary - .NET class for quickly find out Value based on Key and vice versa -
very utilize dictionary<tkey, tvalue>
type store ehh...dictionary type of values, e.g. key = 1, value ="canada"
. in many cases, values in dictionary info unique, keys. , find dictionary<tkey, tvalue>
type, not convenient key value based on value.
my question is, class in .net, suitable in scenario? find out value based on key , vice versa.
this pretty trivial using linq:
int key = new dictionary<int,string>().where((kv)=>kv.value == "somevalue").select(kv=>kv.key).firstordefault();
and extension method if fancy:
static class dictionaryextensions { public static bool tryfindbyvalue<tkey, tvalue>(this idictionary<tkey, tvalue> dict, tvalue value, out tkey key) { var keys = dict.where((kv) => kv.value.equals(value)).select(kv => kv.key); bool retvalue = keys.any(); key = keys.firstordefault(); homecoming retvalue; } }
what can implement idictionary
, delegate 2 dictionary
objects (one key-value mapping , 1 value-key mapping) backing store , have constraint on add
values must unique. way guaranteed there @ 1 entry particular value. edit using 2 dictionaries improve access time(thanks @samstephens) both keys , values stored in ordered hashtable.
.net dictionary
Comments
Post a Comment