Dictionary with delegate or switch?

后端 未结 6 1427
一生所求
一生所求 2021-02-07 01:21

I am writing a parser which calls some functions dependent on some value.

I can implement this logic with simple switch like this:

switch(some_val)            


        
6条回答
  •  醉话见心
    2021-02-07 02:00

    I like the dictionary approach because it gives you the ability to possibly swap out your lambda expressions as the need arises. This is a great trick for a programmer to keep in their tool bag for cases involving programs using complex decision trees. It would be very unwieldy to modify your switch case blocks at run time and the best answer would end up involving lambdas anyway.

    I once had a situation where I needed to create a switch statement dynamically. This method provided a lot more readable solution.

提交回复
热议问题