Friday, October 28, 2016

Fun with Dictionaries – Replacing ugly switch and if statements

Recently I have been using a new technique to replace those ugly if statements and switch statements with a dictionary.  Lets get started with some code first:

So that Build method with all the if statements isn’t very appealing.  Lets make it better by using a switch statement instead:

That is better but I have started to prefer using a dictionary instead.  I think this code looks better:

Adding new items is one line now instead of several with the if and switch statements.

1 comment:

  1. CASINO ROOM, LUXURY, CA (Casino), CA - Mapyro
    CASINO ROOM, 서산 출장마사지 LUXURY, CA 목포 출장마사지 (Casino), CA. The number one spot 오산 출장마사지 for gambling, 의정부 출장마사지 hotel, spa, dining and entertainment. 과천 출장안마

    ReplyDelete