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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:28:06+00:00 2026-05-13T05:28:06+00:00

I am just starting to get to grips with generics and am (ab)using them

  • 0

I am just starting to get to grips with generics and am (ab)using them to refactor a fairly complex section of my code (I’ve only been using c# for a little while but am fairly experienced in other languages).

I have an inheritance structure where my classes extend a base class. In the base class I have most of the functionality implemented. But I want to be able to associate these children classes with instances of their siblings.

Here is a simplification of some of the relevant code:

class ParentClass<T>
    where T : ParentClass<T>, new()
{

    public static T Create()
    {
        return new T();
    }

    private object joinedItem;

    public void Join<TJoinee>(TJoinee item)
        where TJoinee : ParentClass<TJoinee>, new()
    {
        joinedItem = item;
    }

}

class ChildOne : ParentClass<ChildOne>
{
}

class ChildTwo : ParentClass<ChildTwo>
{
}

With this code in place I can do something like:

var a = ChildOne.Create();
a.Join(new ChildTwo());

The problem is that I needed to type joinedItem as object when really I want to type it as ParentClass<Something>. Is it possible to prescribe a more specific type to joinedItem? Or am I just horribly abusing the language and should be taking a completely different approach?

  • 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-13T05:28:06+00:00Added an answer on May 13, 2026 at 5:28 am

    Could you extract an interface from ParentClass<T> that is not dependent on T? That way joinedItem could be typed to the interface.

    That seems to be as far as you can take what it looks like you’re trying to do (join ParentClass<T> instances to ParentClass<U> instances).

    If there is nothing in that ParentClass<T> interface that is not dependant on T, then it will be difficult to find a more useful interface than object without knowing T.

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

Sidebar

Related Questions

I'm just starting to get to grips with ideas and techniques behind complex networks
I'm just starting to get into generating code from a DSL using visual studio
Just starting to get to grips with python and MySQLdb and was wondering Where
Im just starting to get to grips with silverlight 3, coming from ASP.NET and
I have an established Magento site that is only just starting to get complaints
I'm just starting off using linq queries within c#, and i'm struggling to get
Im just starting to get the hang of using json and Id like to
I'm just starting to get to grips with web development and trying to build
I'm trying to get the grips of using prototypal inheritance. Here is the code
I'm just starting to get into jquery/javascript programming so this may be a beginner

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.