There are a lot of situations where LINQ and Lambda expressions can make your code simpler to write and also simpler to read. Today I stumble across an extension of a really old project. This project pass one parameter to a function, and this parameter is a list of comma separated BlockName. Despite the business meaning of a BlockName, I need an extension that permits also to add specific parameters to the block name, so we decided to use a syntax like:
This is really an awkward way to pass parameters around, but these are the requirements, so we cannot question about it. I decided to write some general parameter class that permits me to deal with strongly typed parameters name and value, because I do not want monkeying around with string that I have to parse etc, etc. In 10 minutes I write this simple class:
This method is quite general and can be abstracted in a base class. The core is a dictionary of parameterDefinition, where each element has a string (parameterName) as a key, and an Action<BlockParameters, String> as value. In static constructor I fill this dictionary with my parameters, and I can specify a custom function that convert from string representation to the real value. For the Int64 value in fact, I check if the number if prefixed with 0x to use exadecimal conversion.
With lambda and a little bit of linq the class is simple, easy to read and to use. Here is a typical use
Now you can use the variable p1 with full intellisense, moreover if you specify a parameter that is not permitted it throws an exception, and you have the ability to pass Int64 value both in normal formal and hexadecimal format.