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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:05:03+00:00 2026-05-22T16:05:03+00:00

I have a class hierarchy, which is something like that: public abstract class BaseDecision

  • 0

I have a class hierarchy, which is something like that:

public abstract class BaseDecision 
{
    // <implementation>
}

public class CCDecision : BaseDecision 
{
    // <implementation>
}

public class UCDecision : BaseDecision  
{
    // <implementation>
}

public abstract class BaseInfo<TDecision> where TDecision:BaseDecision, new()
{
    public TDecision proposedDecision;
    public TDecision finalDecision;
    // <implementation>
}

public class CCInfo : BaseInfo<CCDecision>
{
    // <implementation>
}

public class UCInfo : BaseInfo<UCDecision>
{
    // <implementation>
}

The problem is, that with such a class hierarchy, I can’t declare a variable, which could contain instances of both CCInfo and UCInfo classes (as they use a base type with a different type parameter). As far as I understand – I can’t make use of variance either, as my generic parameter is used both for input and for output.

I personally sense some kind of anti-pattern here, but just can’t figure out how to solve this.

  • 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-22T16:05:04+00:00Added an answer on May 22, 2026 at 4:05 pm

    The design really relies on the purpose you want to achieve.
    If you want a single variable able to store instances of both CCInfo and UCInfo classes, this variable will be able to see only what is common to these 2 types.
    It seems to me that the only thing feasible which would be the same whatever it’s a CCInfo or UCInfo is accessing to proposedDecision and finalDecision, seen as BaseDecision instances (nothing more precise if you want to remain generic).
    So in this context these properties are read (are “out properties”).
    Thus you can rely on covariance this way :

    class Program
    {
        public static void Main(string[] args)
        {
            CCInfo ccInfo = new CCInfo();
            UCInfo ucInfo = new UCInfo();
            IBaseInfo<BaseDecision> x = ccInfo;
            x = ucInfo;
        }
    
        public class BaseDecision
        {
            // <implementation> 
        }
        public class CCDecision : BaseDecision
        {
            // <implementation> 
        }
        public class UCDecision : BaseDecision
        {
            // <implementation> 
        }
        public interface IBaseInfo<out TDecision> where TDecision : BaseDecision, new()
        {
            TDecision proposedDecision { get; }
            TDecision finalDecision { get; }
        }
        public abstract class BaseInfo<TDecision> : IBaseInfo<TDecision> where TDecision : BaseDecision, new()
        {
            public TDecision proposedDecision { get; set; }
            public TDecision finalDecision { get; set; }
            // <implementation> 
        }
        public class CCInfo : BaseInfo<CCDecision>
        {
            // <implementation> 
        }
        public class UCInfo : BaseInfo<UCDecision>
        {
            // <implementation> 
        }
    }
    

    Sure it compiles. Now it’s up to you to see if this piece of code is reusable in your specific context to achieve your goals… Good luck !

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

Sidebar

Related Questions

I have something like following: public interface A { .... } public abstract class
I have a quite complex class hierarchy in which the classes are cross-like depending
I have this super class which extends from another class public abstract class AbstractDOEMessageFinderAction
Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in
I have a class hierarchy, this one: type TMatrix = class protected //... public
I have a simple Class Hierarchy that I am trying to get to work
I have a small class hierarchy where I would like to have a child
Let's have the following class hierarchy: public class ParentClass implements SomeInterface { } public
The relevant structures from my cube are that I have a Hierarchy with Class
I have a hierarchy of classes mapped into hibernate that looks kind of like

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.