Check if Key Exists in NameValueCollection

后端 未结 12 703
执念已碎
执念已碎 2020-12-13 16:42

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

12条回答
  •  独厮守ぢ
    2020-12-13 17:19

    From MSDN:

    This property returns null in the following cases:

    1) if the specified key is not found;

    So you can just:

    NameValueCollection collection = ...
    string value = collection[key];
    if (value == null) // key doesn't exist
    

    2) if the specified key is found and its associated value is null.

    collection[key] calls base.Get() then base.FindEntry() which internally uses Hashtable with performance O(1).

提交回复
热议问题