Is there a quick and simple way to check if a key exists in a NameValueCollection without looping through it?
Looking for something like Dictionary.ContainsKey() or similar.
There are many ways to solve this of course. Just wondering if someone can help scratch my brain itch.
In VB it's:
In C# should just be:
Not sure if it should be
null
or""
but this should help.I am using this collection, when I worked in small elements collection.
Where elements lot, I think need use "Dictionary". My code:
Or may be use this:
If the collection size is small you could go with the solution provided by rich.okelly. However, a large collection means that the generation of the dictionary may be noticeably slower than just searching the keys collection.
Also, if your usage scenario is searching for keys in different points in time, where the NameValueCollection may have been modified, generating the dictionary each time may, again, be slower than just searching the keys collection.
Return Value Type: System.Boolean true if the NameValueCollection contains keys that are not null; otherwise, false. LINK
Use this method:
It is the most efficient for
NameValueCollection
and doesn't depend on does collection containnull
values or not.I don't think any of these answers are quite right/optimal. NameValueCollection not only doesn't distinguish between null values and missing values, it's also case-insensitive with regards to it's keys. Thus, I think a full solution would be: