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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:59:05+00:00 2026-05-15T12:59:05+00:00

I am working on a class library and am having some trouble with generics.

  • 0

I am working on a class library and am having some trouble with generics. I have a ITransaction interface which has a collection of ITransactionItem. Each ITranscation can be either a CapitalCall or Distribution. A CapitalCall is a ITransaction but has a few additional properties. A CapitalCallItem is a ITransactionItem with a few additional properties. A CapitalCall has a collection of CapitalCallItems. Likewise, there exists a Distribution class with a collection of DistributionItem.

I have tried making the Transaction interface generic:

interface ITransactionBase<TItem> 
    where TItem: ITransactionItem
{
    List<TItem> ITransactionItems
    {
        get;
        set;
    }

}

This works perfectly when I implement it:

class CapitalCall : ITransactionBase<CapitalCallItem>

Now all of the items in the collection are of type CapitalCallItem.

I run into the following problem. I would like to know the associate ITransaction on a ITranscationItem. I created a property on the ITransactionItem table of type ITranscation. When I use this property, it is no longer typed to the correct class:

var capitalCall = new CapitalCall();
var trans = capitalCall.TransactionItems[0].Transaction;
// trans is now of the base type ITransaction, instead of typed to CapitalCall.

I have tried making the ITransactionLineItem interface use generics as well, but I get into a recursive generic nightmare when I try to declare it. What is the correct way to model this?

Would this work:

interface ITransaction<TAction, TItems>
    where TItems : ITransactionItem<TAction, TItems>
    where TAction : ITransaction<TAction, TItems>

interface ITransactionItem<TAction, TItems>
    where TItems : ITransactionItem<TAction, TItems>
    where TAction : ITransaction<TAction, TItems>

I am confused as to how I could then use the interface by itself- what if I want a collection of mixed ITransactionItem, without specifying a type? Also I should add that I have base Transaction / Transaction item classes that implement the interface, and CapitalCall / Dist inherit from.

  • 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-15T12:59:06+00:00Added an answer on May 15, 2026 at 12:59 pm

    Yes, this sort of mutually recursive generic declaration will work, but it will make things very complicated – I know from experience. If you want an example of something similar, look at this declaration from my protocol buffers port:

    public interface IMessage<TMessage, TBuilder> : IMessage<TMessage>
         where TMessage : IMessage<TMessage, TBuilder> 
         where TBuilder : IBuilder<TMessage, TBuilder>
    

    IBuilder<,> has the equivalent.

    This declaration also demonstrates the answer to your last question: if some parts of your interface don’t need to know the exact type of transaction, you can declare them in a “less generic” base interface. So you could have:

    interface ITransaction<TAction, TItems> : ITransaction
        where TItems : ITransactionItem<TAction, TItems>
        where TAction : ITransaction<TAction, TItems>
    

    for example, where ITransaction is a non-generic interface.

    Again though, this is not for the faint of heart. In my case I can get away with it because almost no-one uses the raw interfaces – all the implementations are autogenerated, and client code uses those non-generic implementations. I would think long and hard before inflicting this on a developer to actually use day to day…

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

Sidebar

Related Questions

No related questions found

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.