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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:25:32+00:00 2026-05-14T14:25:32+00:00

UPDATE: this is a duplicate of Is the StaticFactory in codecampserver a well known

  • 0

UPDATE: this is a duplicate of
Is the StaticFactory in codecampserver a well known pattern?

  • 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-14T14:25:33+00:00Added an answer on May 14, 2026 at 2:25 pm

    Edit: Please note that this answer was given before the question was completely changed over in an edit. Because of that, it now refers to things that were only present in the question as originally stated. I beg your pardon for all the “dangling pointers”. 🙂


    Short answer:

    With the code you’ve posted, I don’t see an alternative to casting to IFoo<T>. If you don’t, the compiler will give a warning (on my machine, at least).

    More elaborate answer:

    Does your code actually have to be that way? More specifically, do you need the cast in question in the first place?

    I assume you are going to call your factory method more or less like this:

    var stringFoo = FooFactory.CreateFoo<string>();
    

    You have to provide the template parameter (string in this case) explicitly because it cannot be derived from any method argument (in this case because there aren’t actually any at all). Obviously, the factory method will return an IFoo<string>.

    Now, since you have to explicitly specify the type at run-time, you could just as well write:

    var stringFoo = StringFoo.Create();
    

    and therefore have a factory method inside StringFoo, like this, that unconditionally does the obvious:

    public class StringFoo : IFoo<string>
    {
        ...
    
        public static StringFoo Create()  // or alternatively, return an IFoo<string>
        {
            return new StringFoo();
        }
    }
    

    By applying this pattern to other IFoo<T> implementations too, this will save you the if chain or switch block inside FooFactory.CreateFoo<T>, make your code easier, and get rid of the necessity to cast (which you are concerned about).

    Don’t get me wrong, I’m aware that factory methods supporting more than one object type are useful in some cases; but it seems in your case it causes more trouble than it’s worth.


    P.S.: You might find one aspect of some IoC containers interesting. They usually need to be configured, and this encompasses a process where you register concrete types (i.e. implementation classes) for abstract interfaces; for example (here using Autofac):

    var builder = new ContainerBuilder();
    builder.RegisterType<StringFoo>().As<IFoo<string>>();
    

    Then later, you can request an object instance of an abstract type:

    using (var container = builder.Build())
    {
        var stringFoo = container.Resolve<IFoo<string>>();
        ...
    }
    

    The Resolve method is the interesting part. You provide it with an abstract type, and using the registered types, it will return a concrete object of type StringFoo. Look into it, if it doesn’t sound like overkill to you! 🙂

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

Sidebar

Related Questions

Duplicate of this question . update - This is not an exact duplicate. See
Update: This question is a duplicate of Are there any programming languages targeting PHP,
Update: This turned into a blog post, with updated links and code, over at
Update: this question, including the title, was rephrased, see history for details I know
How do you update this? I've never seen any current team that actually checks
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: Please read this question in the context of design principles, elegance, expression of
Update: looks like this is indeed a WiX limitation - Cannot add 64bit com+
As made clear in update 3 on this answer , this notation: var hash
I am having virtually the same problem as this: C# Update combobox bound to

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.