Would you like to see a collection which magically creates an item as soon as you ask for it? Well, here it is. The collection is asked to return a value for a key, and volia, you will be informed that the item exists in the collection.
static void Main( string args )
NameValueCollection Collection = new NameValueCollection();
"Does NameValueCollection magically create items? " +
Collection["foo"] != null ? " Yes, it does!" : "No, it doesn't."
Can you spot the problem in the code?
The + operator has higher precedence that the ?: operator, therefore the result of Collection["foo"] is first concatenated with the "Does NameValueCollection magically create items? " string and then compared to null.
Post a Comment