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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:11:12+00:00 2026-05-26T20:11:12+00:00

I know that the title may be confusing (or even misleading), but I’m planning

  • 0

I know that the title may be confusing (or even misleading), but I’m planning to create an interface which is generic, and it should implement a method involving a generic parameter.

When implemented in class AFirst, it should have a method MyMethod<A>() that returns the type A, and when implemented in class BFirst, it should have a method MyMethod<B>() that returns type B. I need this functionality as there is an inheritance relationship between A and B (and MANY others) and I need a generic method that I can call with any of the base classes.

If it was confusing, have a look at what I want to do:

Consider B derives from A.
Consider AFirst and BFirst implement IMyInterface<A> and IMyInterface<B> respectively:

BFirst mySecondObj = new BFirst();
A myA = BFirst.MyMethod<A>();
B myB = BFirst.MyMethod<B>();

I need access to the MyMethod templates for the base classes to, so when I instantiate the BFirst instance, I can call either MyMethod for A or B. I’m building a template system and think these AFirst and BFirst are the templates, and MyMethod acts like a factory method. I will have a big hierarchy, and the project needs to be extensible by deriving even more classes from my base class A, so I can’t just create seperate interfaces or methods for each of them.

I tried this:

interface IMyInterface<T> where T : A
{
    T GetNewInstance<T>();
}

and I tried to implement this way, but I’m getting it as created like this when I click implement:

class AFirst : IMyInterface<A>
{
    public T GetNewInstance<T>()
    {
        throw new NotImplementedException();
    }
}

Didn’t make sense to me in the way that I’ve specified the T type to be A, but it still implements as T. For example, it will go like this (below is how I want it to happen)

class AFirst : IMyInterface<A>
{
    public A GetNewInstance<A>()
    {
        throw new NotImplementedException();
    }
}

class BFirst : AFirst, IMyInterface<B>
{
    public B GetNewInstance<B>()
    {
        throw new NotImplementedException();
    }
}

and from outer code, call the sample as in the beginning of my question:

BFirst myBFirst = new BFirst();
A a = BFirst.GetNewInstance<A>(); //calls AFirst's method and returns A
B b = BFirst.GetNewInstance<B>(); //calls BFirst's method and returns B

How is this possible?
Thanks,
Can.

  • 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-26T20:11:13+00:00Added an answer on May 26, 2026 at 8:11 pm

    In your generic interface you define a generic method. I think that’s where the confusion is. It should be a normal method that returns your generic type. I.e.

    interface IMyInterface<T> where T : A
    {
        T GetNewInstance();
    }
    

    This will get implemented as

    class AFirst : IMyInterface<A>
    {
        public A GetNewInstance()
        {
            throw new NotImplementedException();
        }
    }
    

    which I’m guessing is what you want.

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

Sidebar

Related Questions

Ok. I know the title may be confusing. Logic That I have implemented is
I know that the title may sound absurd, but I don't know ho to
i didn't really know how to title this question, but here's a thing that
the title may not be appropriate because I don't really know how I should
That title is brutal, but I don't know how else to put it. I
I know the title may be funny for you but I am actually facing
The title may be misleading. What I really want is to know the name
Okay, I know that is a funky-sounding title, but I couldn't think of a
I'm here again with another question/problem. I know that topic's title may be like
I know the title may seem strange but this is what I want 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.