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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:39:51+00:00 2026-05-24T12:39:51+00:00

I have this method with a huge switch statement like this: public bool ExecuteCommand(string

  • 0

I have this method with a huge switch statement like this:

public bool ExecuteCommand(string command, string args)
{
    bool result = false;
    switch (command)
    {
        case "command1": result = Method1(args); break;
        case "command2": result = Method2(args); break;
        // etc.
    }
    return result;
}
private bool Method1(string args) {...}

Now I thought about replacing this with a dictionary of Func<> delegates so that I can eliminate the switch statement:

private Dictionary<string, Func<string, bool>> _commands = new ...;
public MyClass()
{
    _commands.Add("command1", Method1);
    // etc:
}
public bool ExecuteCommand(string command, string args)
{
    return _commands[command](args);
}

The problem I see with this, is that a new Dictionary is instantiated and populated with each new instance of MyClass.

Is it possible to somehow make that Dictionary (containing delegates to instance methods) a static member, which would be initialized only once, in the static constructor?

E.g. something like this (does not work):

private static Dictionary<string, Func<string, bool>> _commands = new ...;
static MyClass()
{
    // the following line will result in a compiler error:
    // error CS0120: An object reference is required for the non-static field,
    // method, or property 'MyClass.Method1(string, string)'
    _commands.Add("command1", MyClass.Method1);
}
  • 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-24T12:39:53+00:00Added an answer on May 24, 2026 at 12:39 pm

    You can initialize it in the static constructor – but you’ll need to create instances of MyClass, which may not be what you want, because I assume you want the command to execute “in the context of” the instance which Execute has been called on.

    Alternatively, you can populate the dictionary with delegates which take an instance of MyClass as well, like this:

    class MyClass
    {
        static Dictionary<string, Func<MyClass, string, bool>> commands
            = new Dictionary<string, Func<MyClass, string, bool>>
        {
            { "Foo", (@this, x) => @this.Foo(x) },
            { "Bar", (@this, y) => @this.Bar(y) }
        };
    
        public bool Execute(string command, string value)
        {
            return commands[command](this, value);
        }
    
        public bool Foo(string x)
        {
            return x.Length > 3;
        }
    
        public bool Bar(string x)
        {
            return x == "";
        }
    }
    

    In theory I believe it should be doable without the lambda expression by creating an “open delegate”, but it would need a bit more work using reflection. If you don’t mind the ugliness and tiny performance penalty of the extra indirection, I think this approach should work quite well.

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

Sidebar

Related Questions

I have this method: public bool CanExecute() And after 70 commits, I added an
I have this method in my db class public function query($queryString) { if (!$this->_connected)
So i have this method: internal K GetValue<T, K>(T source, string col) where T
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
I have this method Verify_X which is called during databind for a listbox selected
I have this method: private delegate void watcherReader(StreamReader sr); private void watchProc(StreamReader sr) {
Currently i have this method: static boolean checkDecimalPlaces(double d, int decimalPlaces){ if (d==0) return
I have this function from a plugin (from a previous post) // This method
Does anyone have any thoughts on this method? I have did some performance testing

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.