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 8933773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:37:34+00:00 2026-06-15T09:37:34+00:00

I have a situation: I nee to do something with a class. What should

  • 0

I have a situation:

I nee to do something with a class.

What should be more efficiente, modify the method this way witf IFs or created methos for each action?

public value Value(int command)
        {
            if (command == 1)
            {
                DoSomething1();
            }

            if (command == 2)
            {
                DoSomething2();
            }
            else
            {
                return empty();
            }


        }

There are going to be like 50 o more of this commands.
Whats isbetter in terms of performance on execution and size of the exectuable?

  • 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-06-15T09:37:34+00:00Added an answer on June 15, 2026 at 9:37 am

    At a high-level, it looks like you’re trying to implement some kind of dynamic-dispatch system? Or are you just wanting to perform a specified operation without any polymorphism? It’s hard to tell.

    Anyway, based on the example you’ve given, switch block would be the most performant, as the JIT compiler converts it into an efficient hashtable lookup instead of a series of comparisons, so just do this:

    enum Command { // observe how I use an enum instead "magic" integers
        DoSomethingX = 1,
        DoSomethingY = 2
    }
    
    public Value GetValue(Command command) {
        switch(command) {
            case Command.DoSomethingX: return DoSomethingX();
            case Command.DoSomethingY: return DoSomethingY();
            default: return GetEmpty();
        }
    }
    

    I also note that the switch block also means you get more compact code.

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

Sidebar

Related Questions

i have situation like this: class IData { virtual void get() = 0; virtual
I have situation like this: // Object Class class Person_Object { protected $_id; public
I have this situation where I declare inside my main class a function that
I have situation where in the regular expression is something like this: ^b?A+b?$ So
I can't make git push origin B. I have situation something like this _____________________________________
I have situation like this: user submits form with action='/pay' in '/pay' I have
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()
I have this situation: $(#button).toggle(function(){ $(#window).animate({top:'0%'},1000); },function(){ $(#window).animate({top:'-100%'},1000); }); but I need change it
I have situation where I have to call method of interface using reflection, like
I have situation to extend the Enumerable class in c# to add the new

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.