Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4039424
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:36:35+00:00 2026-05-20T12:36:35+00:00

I came across some code recently that replaces the use of switches by hard-coding

  • 0

I came across some code recently that replaces the use of switches by hard-coding a

Dictionary<string (or whatever we would've been switching on), Func<...>> 

and where ever the switch would’ve been, it instead does dict[“value”].Invoke(…).
The code feels wrong in some way, but at the same time, the methods do look a bit cleaner, especially when there’s many possible cases. I can’t give any rationale as to why this is good or bad design so I was hoping someone could give some reasons to support/condemn this kind of code. Is there a gain in performance? Loss of clarity?

Example:

public class A {
    ...
    public int SomeMethod(string arg){
        ...
        switch(arg) {
            case "a": do stuff; break;
            case "b": do other stuff; break;
            etc.
        }
        ...
    }
    ...
}

becomes

public class A {

    Dictionary<string, Func<int>> funcs = new Dictionary<string, Func<int>> {
        { "a", () => 0; },
        { "b", () => DoOtherStuff(); }
        ... etc.
    };

    public int SomeMethod(string arg){
        ...
        funcs[arg].Invoke();
        ...
    }
    ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:36:35+00:00Added an answer on May 20, 2026 at 12:36 pm

    Advantages:

    1. You can change the behaviour at runtime of the “switch” at runtime
    2. it doesn’t clutter the methods using it
    3. you can have non-literal cases (ie. case a + b == 3) with much less hassle

    Disadvantages:

    1. All of your methods must have the same signature.
    2. You have a change of scope, you can’t use variables defined in the scope of the method unless you capture them in the lambda, you’ll have to take care of redefining all lambdas should you add a variable at some point
    3. you’ll have to deal with non-existant indexes specifically (similar to default in a switch)
    4. the stacktrace will be more complicated if an unhandled exception should bubble up, resulting in a harder to debug application

    Should you use it? It really depends. You’ll have to define the dictionary at some place, so the code will be cluttered by it somewhere. You’ll have to decide for yourself. If you need to switch behaviour at runtime, the dictionary solution really sticks out, especially, if the methods you use don’t have sideeffects (ie. don’t need access to scoped variables).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was working on some code recently and came across a method that had
An interesting issue came up recently. We came across some code that is using
Whilst refactoring some code I came across some getter methods that returns a std::string.
I came across some code recently that used a custom error handler to turn
I recently came across some code that looked like: if(sizeof(var,2) == 4) { ...
I recently came across some Java code that simply put some strings into a
I recently came across this in some code - basically someone trying to create
A while ago, I came across some code that marked a data member of
I am modifying some code and came across a declaration that I am having
We recently came across some sample code from a vendor for hashing a secret

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.