[rant] C# KeyValuePair.Value = (?)

(A couple of years ago I’d be happily coding around the issue at the moment but you know, Ruby makes you a spoiled bastard: my tolerance level for language WTFs has become zero, so I’m here ranting about this)

So I’ve got my nice ICollections and I’m merrily passing them around and binding them to WPF ListBoxes’ ItemSource and stuff and one of the ICollections happens to be an IDictionary. “No worries”, i thought, “It’s still an ICollection so i don’t see the problem” *type type ItemsSource=”{Binding Path=MyDictionary}” type type*. An F5 later this nice RuntimeException pats me on the back:

A TwoWay or OneWayToSource binding cannot work on the read-only property 'Value' of type 'System.Collections.Generic.KeyValuePair`2

Really. You’ve gotta be shitting me. I mean: what happened is that I’m binding the Value property of each element to an editable property and the guys who designed the KeyValuePair class thought it was a good idea to make its Value property readonly. And it’s not over: if you try to inherit KeyValuePair to fix this WTF yourself you’ll find out that the class is Sealed. I am very aware that writing your personal framework class to save yourself some jumping through hoops is generally a Bad Idea but I feel that if I want to I should definitely be allowed to do that.

Is there a vaild reason I don’t know about that justifies this? If so, please let me know!



  1. Pingback: [rant] C# KeyValuePair.Value = (?) « using (IMHO) | Source code bank
  2. Gunnar · June 21, 2011

    I’m just annoyed about the same thing, but I can only assume, that it’s meant to prevent you to alter anything on a KeyValuePairs that are just shadow copies from the actual data in the collections.
    But yeah, a good rant is healthy sometimes. I rant with you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s