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

  • SEARCH
  • Home
  • 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 1050215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:44:13+00:00 2026-05-16T16:44:13+00:00

Currently, I have a static factory method like this: public static Book Create(BookCode code)

  • 0

Currently, I have a static factory method like this:

public static Book Create(BookCode code) {
    if (code == BookCode.Harry) return new Book(BookResource.Harry);
    if (code == BookCode.Julian) return new Book(BookResource.Julian);
    // etc.
}

The reason I don’t cache them in any way is because BookResource is sensitive to culture, which can change between the calls. The changes to culture needs to reflect in the returned book objects.

Doing those if-statements is possible a speed bottleneck. But what if we map book codes to anonymous functions/delegates? Something like the following:

delegate Book Create();
private static Dictionary<BookCode, Delegate> ctorsByCode = new Dictionary<BookCode, Delegate>();
// Set the following somewhere
// not working!
ctorsByCode[BookCode.Harry] = Create () => { return new Book(BookResource.Harry); }
// not working!
ctorsByCode[BookCode.Julian] = Create () => { return new Book(BookResource.Julian); } 
public static Book Create(BookCode code) {
    return (Book)ctorsByCode[code].DynamicInvoke(null);
}

How could I get those Create() => { lines to actually work?

Is this worth it speed-wise, when there are <50 book codes (thus <50 if-statements)?

This is a similar question, but unfortunately the author doesn’t post his code Enum, Delegate Dictionary collection where delegate points to an overloaded method

Update

Did some performance benchmarks ifs vs delegates. I picked the unit code randomly and used the same seed for both methods. The delegate version is actually slightly slower. Those delegates are causing some kind of overhead. I used release builds for the runs.

5000000 iterations
CreateFromCodeIf ~ 9780ms
CreateFromCodeDelegate ~ 9990ms
  • 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-16T16:44:14+00:00Added an answer on May 16, 2026 at 4:44 pm

    Like this:

    private static readonly Dictionary<BookCode, Func<Book>> ctorsByCode = new Dictionary<BookCode, Func<Book>>();
    
    ...
    
    ctorsByCode[BookCode.Harry] = () => new Book(BookResource.Harry);
    
    public static Book Create(BookCode code) {
        return ctorsByCode[code]();
    }
    

    The Func<Book> type is a pre-defined generic delegate type that you can use instead of creating your own.
    Also, your dictionary must contain specific a delegate type, not the base Delegate class.

    Although lambda expressions are untyped expressions, they can be used in a place where a delegate type is expected (such as your indexer assignment) and will implicitly assume the type of the delegate.

    You could also write

    ctorsByCode[BookCode.Harry] = new Func<Book>(() => new Book(BookResource.Harry));
    

    I would recommend changing your dictionary to contain BookResources instead, like this:

    private static readonly Dictionary<BookCode, BookResource> codeResources = new Dictionary<BookCode, BookResource>();
    
    ...
    
    codeResources[BookCode.Harry] = BookResource.Harry;
    
    public static Book Create(BookCode code) {
        return new Book(codeResources[code]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this working but it requires me to have a static method
I currently have the following code: public static int currentTimeMillis() { long millisLong =
I currently have this Fluent NHibernate configuration: public class NHibernateConfig { public static Configuration
I currently have a function: public static Attribute GetAttribute(MemberInfo Member, Type AttributeType) { Object[]
I currently have code that does the following: private final static ExecutorService pool =
I have some code which I'm currently using to change the static-IP of a
I'm attempting to unit test some code using NUnit. I have a method: public
I currently have this: #define THIS(T) (boost::static_pointer_cast<T>(shared_from_this())) The macro is used in a method
I currently have a helper class that I am using to obfuscate a static
For some user controls, I have this binding: AppLanguage={Binding Path=ApplicationLanguage, Source={x:Static Application.Current}} This works

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.