Click here to Skip to main content
13,148,434 members (65,931 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 17 Jul 2014

Switch statement alternative

, 22 Jul 2014
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

You may also be interested in...

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
memberahagel23-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
memberahagel24-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 Sawant23-Jul-14 2:54
professionalNitin Sawant23-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 Sawant23-Jul-14 2:55
professionalNitin Sawant23-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 Sawant23-Jul-14 2:57
professionalNitin Sawant23-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
professionalPaulo Zemek18-Jul-14 4:52 
AnswerRe: A List is better in this case. Pin
Member 1058457421-Jul-14 1:24
memberMember 1058457421-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 Sawant20-Jul-14 19:42
professionalNitin Sawant20-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.

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