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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:09:36+00:00 2026-06-18T01:09:36+00:00

In C#, is it possible to write something like this: public class MyClass<T> :

  • 0

In C#, is it possible to write something like this:

public class MyClass<T> : T 
    where T : class, new() 
{
}

I know that the above implementation does not compile, but what I am actually trying to achive is implementing some kind of generic wrapper to an unknown type, so that an client can call the wrapper just as he would call the type, provided by the parameter T, instead of calling it using something like wrapper.Instance.SomeMember().

Thanks in advance!

  • 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-06-18T01:09:38+00:00Added an answer on June 18, 2026 at 1:09 am

    This isn’t possible.

    In my opinion, I don’t think that a wrapper should be implemented using inheritance.

    For example, let’s say we’ve an Engine class and you need to implement a FerrariEngine. And you have a Car class.

    You’re saying that Car should inherit FerrariEngine. It looks terrible for me!

    At the end of the day, you’re looking to do something like dependency injection using inheritance and, again, this isn’t the right path.

    My suggestion is don’t try to make your life easier: decide an architecture based on rational points.

    UPDATE

    The OP said in some comment:

    I want to make this class to manage instances of objects of type T, so
    that the client does not need to take care of when the instances need
    to be created.

    You don’t need to make strange things to get what you want:

    public interface IEngine 
    {
         void Start();
    }
    
    public sealed class FerrariEngine : IEngine
    {
         public FerrariEngine()
         {
              Start();
         }
    
         public void Start()
         {
         }
    }
    
    public abstract class Car<TEngine> where TEngine: IEngine, new()
    {
        public Car()
        {
            _engine = new Lazy<TEngine>(() => new TEngine());
        }
    
        private readonly Lazy<TEngine> _engine;
    
        public TEngine Engine
        {
            get { return _engine.Value; }
        }
    }
    
    public class FerrariCar : Car<FerrariEngine>
    {
    }
    

    Finally, if we create an instance of FerrariCar:

    Car<FerrariEngine> myFerrari = new FerrariCar();
    

    The engine will be instantiated and started, without developer intervention!

    Check how Lazy<T> and basic generic constraints make the job 😉

    In summary:

    • Using Lazy<T> the engine will be instantiated only when some access the Engine property.
    • Once the lazy-loaded engine is instantiated, since FerrariEngine implements a parameterless constructor calling Start() itself, it will start the engine.

    I believe that this sample illustrates you how you can get what you’re looking for and using C# “as is”!

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

Sidebar

Related Questions

I'm trying to build an object that looks something like this: public class MyObject
It seems to be possible in Java to write something like this: private enum
I'm using propel master-dev with symfony 2.1. Is possible to write something like that
Usually when I write a PHP class I have done something like this: class
I'm interested in calculating permutations for parameters for something like this: public class Movie()
Is it possible to write something similar to mem_fun and bind1st etc. so that
I generate a midi file and write it like this: File output = new
I have an object like this public class Employee { public String getName() {
I would like to write this: template<typename T1, typename T2> class OK { T1
Is it possible to write GLSL ES fragment shaders under iOS that generate multiple

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.