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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:15:53+00:00 2026-05-15T21:15:53+00:00

Is there a way to do something like this in c#? Consider the following

  • 0

Is there a way to do something like this in c#? Consider the following example and assume that Child1, Child2, Child3 are all children of Parent –

class Class1
{

  SomeObject< Parent > mSomeObject; 

  Class1()
  {

     if (condition1)

          mSomeObject = new SomeObject<Child1>();

     else if (condition2) 

          mSomeObject = new SomeObject<Child2>(); 

     else if (condition3) 

          mSomeObject = new SomeObject<Child3>();

  }

}

The idea is that that Class1 would have SomeObject as a member, but it is uncertain until runtime what generic form of SomeObject it should take. Any help would be appreciated. Thanks!

  • 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-15T21:15:53+00:00Added an answer on May 15, 2026 at 9:15 pm

    You should use interface based inheritance. This will allow child1, child2, and child3 to be polymorphic and take on the characteristics of the parent without the need for such guard logic. With the IF tests gone your code will be more readable and easier to modify later.

    Here’s and example I just wrote with LINQPad to show this in action.

    public interface ICar
    {
        bool IsAutomatic();
    }
    
    public class Silverado : ICar
    {
        public bool IsAutomatic()
        {
            return true;
        }
    }
    
    public class Semi : ICar
    {
        public bool IsAutomatic()
        {
            return false;
        }
    }
    
    void Main()
    {
        ICar car = new Silverado();
        bool isAuto = car.IsAutomatic();
        isAuto.Dump();
    
        car = new Semi();
        isAuto = car.IsAutomatic();
        isAuto.Dump();
    }
    

    OUTPUT:

    True
    False
    

    I prefer interface based inheritance as opposed to abstract classes as described by AllenG. Reasons such as multiple inheritance – a class can implement many interface but only inherit from 1 class.

    Hope this helps…

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

Sidebar

Related Questions

In Ant is there any way to do something like this: <arguments id=arg-list> <arg
Is there a way to execute code after each Terminal? So that something like
Is there any way to have something that looks just like a file on
Is there some easy way to pad Strings in Java? Seems like something that
Is there a way to pass null arguments to C# methods (something like null
Is there some standard way or has anyone written something that allows you to
Is there a way to paste something at the end of a line?
Is there a way to get a DrawingContext (or something similar) for a WriteableBitmap
Is there any way to use AppleScript (or something else) to query currently running
Is there a way to implement/use lambda functions in bash? I'm thinking of something

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.