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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:13:05+00:00 2026-05-27T15:13:05+00:00

I have a base abstract class with several classes which extend and override it,

  • 0

I have a base abstract class with several classes which extend and override it, however they all have the same constructor. Right now I call each constructor from a switch statement:

case 1: return new A(arg1); // arg is int

etc., for about 10 classes. Is there a way with generics or delegates to make one method which would take a classname and instantiate that type with the arguments?

This would help, since if I make a change to the constructors I also have to change each instantiation.

A delegate would be most straight-forward but research says no to assigning constructors to delegates.

ADDITION:
I am now on wifi with my laptop and not just my cell, so here comes some code.

This is an example of what happens now:

switch (new Random().Next(4))
{
    case 3:
         // classA : baseClass, args{int, bool, otherClass}
         return new classA(arg1, arg2, arg3);
    case 2:
         // classB : baseClass, args{int, bool, otherClass}
         return new classB(arg1, arg2, arg3);
    case 1:
         // classC : baseClass, args{int, bool, otherClass}
         return new classC(arg1, arg2, arg3);
    case 0:
         // classD : baseClass, args{int, bool, otherClass}
         return new classD(arg1, arg2, arg3);
    default:
         continue;
}

I would like to call one instantiator at the end of the block with the three arguments.

A solution I thought of is to make and instantiator class that works similar to this:

ClassInstantiator inst = new ClassInstantiator(arg1, arg2, arg3);
switch(new Random().Next(4))
{
     case 4:
         return inst.instantiate<classA>();
     ...
}

Is this already built in to the language, or does anyone know of a more elegant solution?

  • 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-27T15:13:06+00:00Added an answer on May 27, 2026 at 3:13 pm

    The only constructor-related constraint you can put on a generic type is the new() constraint, which requires a public parameterless constructor.

    You could use reflection; the example assumes that all the related types have a constructor taking an int, as in your example, and that the abstract base class is called B:

    public T CreateInstance<T>(int i) where T : B
    {
        return (T)Activator.CreateInstance(typeof(T), i);
    }
    

    Then you’d call it like this:

    A a = CreateInstance<A>(3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several Abstract Base Classes which act like interfaces as known from Java
I have an abstract base class which acts as an interface. I have two
I have an abstract base class called Shape from which both Circle and Rectangle
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a abstract base class A and a set of 10 derived classes.
I have a abstract base class that I have many inherited classes coming off
In C#, I have a class hierarchy with a couple of abstract base classes
An abstract base class (interface class) usually has all its member functions abstract. However,
I have a common assembly/project that has an abstract base class, then several derived
I have an interface IServiceInfo and an abstract class ServiceInfo. There are several classes

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.