Click here to Skip to main content
15,032,755 members
Articles / Programming Languages / C#
Tip/Trick
Posted 17 Jul 2014

Tagged as

Stats

43.1K views
10 bookmarked

Switch statement alternative

Rate me:
Please Sign up or sign in to vote.
2.93/5 (11 votes)
22 Jul 2014CPOL
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)

Share

About the Author

Nitin S
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
SuggestionThe switch guys are missing the point Pin
jimibt27-May-15 21:15
Memberjimibt27-May-15 21:15 
SuggestionAnother valid case Pin
Tomas Ruksenas20-Aug-14 21:31
MemberTomas 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
MemberJohn 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
MemberFrederico 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
MemberFrederico Barbosa29-Jul-14 1:49 
QuestionCan't Linq be used here? Pin
George Swan22-Jul-14 21:02
MemberGeorge 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 Pin
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
MemberFatCatProgrammer23-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
MemberJohn 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
professionalSimon_Whale21-Jul-14 3:41 
QuestionWouldn't this be an if ... else rather than a switch? Pin
PeejayAdams21-Jul-14 3:33
MemberPeejayAdams21-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
Memberhelcode21-Jul-14 1:24 
GeneralRe: A List is better in this case. Pin
leppie21-Jul-14 1:31
Memberleppie21-Jul-14 1:31 
AnswerRe: A List is better in this case. Pin
James Curran21-Jul-14 2:09
MemberJames Curran21-Jul-14 2:09 
QuestionYou need a default case Pin
James Curran18-Jul-14 4:29
MemberJames 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.