09 March 2015

Elegant way to SWITCH & If Else





Switch Case is one of the most traditional way of having conditional break up performed. Here is the new way which directs, delegates and performs the action as needed.

Ideally I have seen developer opting for If Else statements, adding my source code were I need to set a check bit based on the parameter Range.



int mynumbercheck=1000;
//Your number to be checked
var myswitch = new Dictionary <Func<int,bool>, Action>
            { 
             { x => x < 10 ,    () => //Do this!...  },  
             { x => x < 100 ,    () => //Do this!...  },
             { x => x < 1000 ,    () => //Do this!...  },
             { x => x < 10000 ,   () => //Do this!... } ,
             { x => x < 100000 ,  () => //Do this!... },
             { x => x < 1000000 ,  () => //Do this!... } 
            };
Now to call our conditional switch

   myswitch.First(sw => sw.Key(mynumbercheck)).Value();

Hope this Help you and reduces if else code block

12 comments:

  1. Excellent!
    Thanks, this resolved the problem with the range of numbers without "If" o "Switch" sentences :)

    ReplyDelete
  2. This is indeed an elegant solution...I used a Console.WriteLine just to see if it worked. I'd like to use the found value as a variable, but the Action delegate doesn't return the value. How can I do this?

    ReplyDelete
    Replies
    1. public static void Main()
      {
      var mynumbercheck = 40;

      var myswitch = new Dictionary , Action>()
      {
      { x => x < 8.33, () => Console.WriteLine("1") },
      { x => x < 16.66, () => Console.WriteLine("2")},
      { x => x < 25.00, () => Console.WriteLine("3")},
      { x => x < 33.33, () => Console.WriteLine("4")},
      { x => x < 41.66, () => Console.WriteLine("5")},
      { x => x < 50.00, () => Console.WriteLine("6")},
      { x => x < 58.33, () => Console.WriteLine("7")},
      { x => x < 66.66, () => Console.WriteLine("8")},
      { x => x < 75.00, () => Console.WriteLine("9")},
      { x => x < 83.33, () => Console.WriteLine("10")}
      };

      myswitch.First(sw => sw.Key(mynumbercheck)).Value();
      }

      Delete
    2. Hi Dino,
      Here is the updated code for you..
      var mynumbercheck = 40;
      //updated from by Akshaya
      var myswitch = new Dictionary, Action>
      {
      { x => x < 8.33, () => {Console.WriteLine("1");} },
      { x => x < 16.66, () => {Console.WriteLine("2");}},
      { x => x < 25.00, () => {Console.WriteLine("3");}},
      { x => x < 33.33, () => {Console.WriteLine("4");}},
      { x => x < 41.66, () => {Console.WriteLine("5");}},
      { x => x < 50.00, () => {Console.WriteLine("6");}},
      { x => x < 58.33, () => {Console.WriteLine("7");}},
      { x => x < 66.66, () => {Console.WriteLine("8");}},
      { x => x < 75.00, () => {Console.WriteLine("9");}},
      { x => x < 83.33, () => {Console.WriteLine("10");}}
      };

      myswitch.First(sw => sw.Key(Convert.ToInt32(mynumbercheck))).Value();
      Console.ReadLine();

      Delete
    3. There seems to be a Typo
      The Action operator should look like this
      --New Dictionary < Func < int,bool>, Action>

      Delete
  3. This is cool, i tried it and it work, but i just a beginner, can you explian me about those keyword combination that make up var myswitch..
    Thank you..

    ReplyDelete
    Replies
    1. Glad to know things worked out for you.
      variables are assigned based on the Action and Delegate (how Switch-Case Works)
      For more please ref SDK's
      Thanks Akshaya

      Delete
  4. So beautiful, must to have in your toolbox

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. While this may work sometimes, even most of the time, iterating over the items in a Dictionary is not guaranteed to be the same order they were added.
    From https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.7.2
    For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair structure representing a value and its key. The order in which the items are returned is undefined.

    There is no reason to use a Dictionary, to get consistent behavior, a List of KeyValuePairs could be used instead and order will be maintained.

    ReplyDelete

Thanks for your valuable comments

Rate Now: