Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#
Tip/Trick

Switch statement alternative

Rate me:
Please Sign up or sign in to vote.
2.93/5 (11 votes)
22 Jul 2014CPOL 56.2K   10   24
switch statement alternative using anonymous code

Background

Switch statement alternative using Func type

Using the code

You can use a Dictionary to create a mapping of Condition => Action.

class Programm
{
    static void Main()
    {
        Func<int, bool> Default = (x=> true);
        
        var myNum = 1200;
        var cases = new Dictionary<Func<int, bool>, Action>
        {
            { x => x < 3 ,    () => Console.WriteLine("Smaler than 3")   } ,
            { x => x < 30 ,   () => Console.WriteLine("Smaler than 30")  } ,
            { x => x < 300 ,  () => Console.WriteLine("Smaler than 300") },
            { Default,        () => Console.WriteLine("Default case") }
        };
        
        cases.First(kvp => kvp.Key(myNum)).Value();
    }
}

This technique is not a general alternative to switch statement.

Suppose you have a workflow system that is required to perform validation of a data item. Different validation routines are executed based upon some state of the data being processed. Using this technique means
a) You do not have a switch statement with over 100 case statements - very difficult to read.
b) You do not have to include conditional logic in the case statement for each validation routine - very difficult to maintain.
c) You can offload the validation routines and conditional logic into a database where the customer can then update when a validation routine executes as per their requirements - very flexible.

 

 

Thanks for the comments @John B. Oliver

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionThe switch guys are missing the point Pin
jimibt27-May-15 21:15
jimibt27-May-15 21:15 
SuggestionAnother valid case Pin
Tomas Ruksenas20-Aug-14 21:31
Tomas Ruksenas20-Aug-14 21:31 
GeneralMy vote of 1 Pin
ahagel23-Jul-14 18:06
professionalahagel23-Jul-14 18:06 
GeneralRe: My vote of 1 Pin
John B Oliver24-Jul-14 12:07
John B Oliver24-Jul-14 12:07 
GeneralRe: My vote of 1 Pin
ahagel24-Jul-14 13:32
professionalahagel24-Jul-14 13:32 
QuestionWhat about letting the compiler do its magic on switch statements? Pin
Frederico Barbosa22-Jul-14 22:51
Frederico Barbosa22-Jul-14 22:51 
AnswerRe: What about letting the compiler do its magic on switch statements? Pin
Nitin S23-Jul-14 2:54
professionalNitin S23-Jul-14 2:54 
GeneralRe: What about letting the compiler do its magic on switch statements? Pin
Frederico Barbosa29-Jul-14 1:49
Frederico Barbosa29-Jul-14 1:49 
QuestionCan't Linq be used here? Pin
George Swan22-Jul-14 21:02
mveGeorge Swan22-Jul-14 21:02 
AnswerRe: Can't Linq be used here? Pin
Nitin S23-Jul-14 2:55
professionalNitin S23-Jul-14 2:55 
GeneralMy vote of 1 PinPopular
Dan Colasanti21-Jul-14 11:41
professionalDan Colasanti21-Jul-14 11:41 
GeneralRe: My vote of 1 Pin
Nitin S23-Jul-14 2:57
professionalNitin S23-Jul-14 2:57 
GeneralRe: My vote of 1 Pin
FatCatProgrammer23-Jul-14 5:53
FatCatProgrammer23-Jul-14 5:53 
GeneralRe: My vote of 1 Pin
Dan Colasanti23-Jul-14 7:22
professionalDan Colasanti23-Jul-14 7:22 
GeneralRe: My vote of 1 Pin
John B Oliver24-Jul-14 12:23
John B Oliver24-Jul-14 12:23 
GeneralRe: My vote of 1 Pin
Dan Colasanti24-Jul-14 16:05
professionalDan Colasanti24-Jul-14 16:05 
Questionquestion Pin
Simon_Whale21-Jul-14 3:41
Simon_Whale21-Jul-14 3:41 
I would like to know why should I use this over a traditional switch statement?
What are the benefits? Are the benefits purely coding cosmetics? or is there a performance / scaling advantage?
Every day, thousands of innocent plants are killed by vegetarians.

Help end the violence EAT BACON

QuestionWouldn't this be an if ... else rather than a switch? Pin
PeejayAdams21-Jul-14 3:33
PeejayAdams21-Jul-14 3:33 
QuestionA List is better in this case. Pin
Paulo Zemek18-Jul-14 4:52
mvaPaulo Zemek18-Jul-14 4:52 
AnswerRe: A List is better in this case. Pin
helcode21-Jul-14 1:24
helcode21-Jul-14 1:24 
GeneralRe: A List is better in this case. Pin
leppie21-Jul-14 1:31
leppie21-Jul-14 1:31 
AnswerRe: A List is better in this case. Pin
James Curran21-Jul-14 2:09
James Curran21-Jul-14 2:09 
QuestionYou need a default case Pin
James Curran18-Jul-14 4:29
James Curran18-Jul-14 4:29 
AnswerRe: You need a default case Pin
Nitin S20-Jul-14 19:42
professionalNitin S20-Jul-14 19:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.