Anonymous objects as key-value list

Using anonymous objects as key-value pair lists

Lately, I have been using anonymous object quite alot as a way to pass in a number of key-value pairs into methods. This (I believe) is a quite nice way to expose a way to send an unknown number of key-value pairs into a method.

I first encountered this in ASP.NET MVC where it can be used for passing in route data or html attributes to generate an ActionLink. I tend to use it mostly when I need to work with unknown key-value pairs, such as html attributes or sql parameters.

Here’s an example of how to use an anonymous object as a way of passing parameters into a sql call (using SqlCe in this example).