Click here to Skip to main content
Click here to Skip to main content

Tagged as

Switch statement alternative

, 22 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
switch statement alternative using anonymous code


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


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


About the Author

Nitin Sawant
Software Developer (Senior)
India India
I’m a .NET Developer working for a private IT firm, I love the creative software development process and each completed project causes me to be more daring and inspired to reach a new threshold in terms of design and development:
I have 4+ years experience in professional application development using Microsoft Technologies. Implementation of object oriented N-tier scalable architecture.
Expert level programming knowledge in C#, ASP.NET, T-SQL, JavaScript, Jquery, AJAX, WCF, LINQ and Entity framework.
Rich experience in all stages of SDLC including requirement gathering, designing & documenting architecture, detailed client-server, development, testing, and implementation.
Good knowledge of object-oriented concepts, design patterns and a fair understanding of component based architecture and database systems.
Excellent analytical and communication skills.
Quick learner and good performer both in team and independent job environments
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

SuggestionAnother valid case PinmemberTomas Ruksenas20-Aug-14 22:31 
GeneralMy vote of 1 Pinmemberahagel23-Jul-14 19:06 
GeneralRe: My vote of 1 PinmemberJohn B Oliver24-Jul-14 13:07 
GeneralRe: My vote of 1 Pinmemberahagel24-Jul-14 14:32 
QuestionWhat about letting the compiler do its magic on switch statements? PinmemberFrederico Barbosa22-Jul-14 23:51 
AnswerRe: What about letting the compiler do its magic on switch statements? PinprofessionalNitin Sawant23-Jul-14 3:54 
GeneralRe: What about letting the compiler do its magic on switch statements? PinmemberFrederico Barbosa29-Jul-14 2:49 
QuestionCan't Linq be used here? PinmemberGeorge Swan22-Jul-14 22:02 
AnswerRe: Can't Linq be used here? PinprofessionalNitin Sawant23-Jul-14 3:55 
GeneralMy vote of 1 PinprofessionalDan Colasanti21-Jul-14 12:41 
GeneralRe: My vote of 1 PinprofessionalNitin Sawant23-Jul-14 3:57 
GeneralRe: My vote of 1 PinmemberFatCatProgrammer23-Jul-14 6:53 
GeneralRe: My vote of 1 PinprofessionalDan Colasanti23-Jul-14 8:22 
GeneralRe: My vote of 1 PinmemberJohn B Oliver24-Jul-14 13:23 
GeneralRe: My vote of 1 PinprofessionalDan Colasanti24-Jul-14 17:05 
Questionquestion PinprofessionalSimon_Whale21-Jul-14 4:41 
QuestionWouldn't this be an if ... else rather than a switch? PinmemberPeejayAdams21-Jul-14 4:33 
QuestionA List is better in this case. PinprofessionalPaulo Zemek18-Jul-14 5:52 
AnswerRe: A List is better in this case. PinmemberMember 1058457421-Jul-14 2:24 
GeneralRe: A List is better in this case. Pinmemberleppie21-Jul-14 2:31 
AnswerRe: A List is better in this case. PinmemberJames Curran21-Jul-14 3:09 
QuestionYou need a default case PinmemberJames Curran18-Jul-14 5:29 
AnswerRe: You need a default case PinprofessionalNitin Sawant20-Jul-14 20:42 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 22 Jul 2014
Article Copyright 2014 by Nitin Sawant
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid