c# - Check for key in pre-existing dictionary in case insensitive manner -


i want check if dictionary given me contains particular string key. need make check in case insensitive manner. example if passes me http request object has dictionary of strings called headers. need able check if "content-type" or "content-type" or "content-type" key in request.headers dictionary.

the usual containskey() not work since think checks key in case sensitive manner.

i know there exist ways work on defining dictionary case insensitive. here not have control on kind of dictionary passed me.

you have 2 options avaliable you, since don't have control on how dictionary constructed:

iterate entire dictionary's pairs find given key:

var match = dictionary.where(pair => string.equals(pair.key, "hello"     , stringcomparison.invariantcultureignorecase)).firstordefault(); 

or create new dictionary own comparer:

var casesensitivedictionary = new dictionary<string, string>(dictionary     , stringcomparer.invariantcultureignorecase); 

the given comparer going used in creating hashes keys added, if dictionary constructed other comparer hash of given string using comparer won't match non-case-sensitive comparer, can't you.

if you're going checking dictionary once you're better off iterating through keys. if going performing more 1 search @ time using newly constructed dictionary possibly worthwhile.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -