[Mono-list] Re: Checking key existance in a NameValueCollection

Shawn Vose mono@programmerforrent.com
Wed, 13 Oct 2004 12:30:53 -0400


That is interesting. I was going off of memory and I thought c# at one 
time  threw an error for keys not present in a collection. Sorry for any 
confusion and thank you for the clarification.

Regards,
Shawn
Arnaud Bienvenu wrote:

> Shawn Vose a écrit :
>
>> I think it should be in a try statement because if the key doesnt 
>> exist it will throw an  exception.
>
>
> No, it will not. This is from NameValueCollection.Get documentation :
>
> "Return Value : A String that contains a comma-separated list of the 
> values associated with the specified key from the NameValueCollection, 
> if found; otherwise, a null reference (Nothing in Visual Basic)."
>
> I guess the [] operator uses the Get method. I made a test, it does 
> not throw any exception when I read Request.Form["nonexistantkey"]. 
> You may have mixed up things with another cool language, Python, where 
> it would throw a KeyError exception.
>
>> Additionally in c# it would use [] instead of ()
>
>
> This is right :)
>