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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:32:45+00:00 2026-05-18T20:32:45+00:00

In the following simplified example, I need to abstract Collection classes in such a

  • 0

In the following simplified example, I need to abstract Collection classes in such a way that PrintFruitsAndEaters(oranges, orangeEaters); or PrintFruitsAndEaters(apples, appleEaters);
becomes possible:

abstract class Fruit
abstract class FruitEater

class Apple : Fruit
class AppleEater : FruitEater
class Orange : Fruit
class OrangeEater : FruitEater

class AppleCollection : List<Apple>
class OrangeCollection : List<Orange>
class AppleEaterCollection : List<AppleEater>
class OrangeEaterCollection : List<OrangeEater>

I’ve tried templatizing the method and collection classes, but i need to access methods specific to Fruit and FruitEater classes:

class FruitCollection<T> : List<T>
class FruitEaterCollection<T> : List<T>

void PrintFruitsAndEaters<T, S>(FruitCollection<T> fruits, FruitEaterCollection<S> eaters)
  • 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-18T20:32:45+00:00Added an answer on May 18, 2026 at 8:32 pm

    Then you want this:

    void PrintFruitsAndEaters<T, S>(
        FruitCollection<T> fruits,
        FruitEaterCollection<S> eaters)
            where T : Fruit
            where S : FruitEater
    {
        // ...
    }
    

    This will constrain the T and S types as you require; calling the method with a FruitCollection<T>, where the T cannot be guaranteed to be Fruit or a subtype, will result in a compile-time error. Same with S and FruitEater.

    Because of the constraint, when working with a value of type T you will be able to access members of the Fruit class, and when working with a value of type S you will be able to access members of the FruitEater class.

    Note that, as per Brian’s (deleted) answer, you might want to add constraints to the collection types too:

    class FruitCollection<T> : List<T> where T : Fruit { }
    class FruitEaterCollection<T> : List<T> where T : FruitEater { }
    

    But this will still not allow you to omit the constraints on the method.

    (Also, inheriting List<T> is an evil thing to do, instead use IList<T>)

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

Sidebar

Related Questions

My simplified and contrived example is the following:- Lets say that I want to
consider the following simplified example: public class Ticket { public int Id; public TicketState
Let's suppose I have a the following simplified example database consisting of three tables:
I have a list of records with the following structure: (Simplified example!) class Rate
I'm trying to parse some HTML with XPath. Following the simplified XML example below,
To give a simplified example, I've got the following block repeated on the page
I need to generate the following in ASP.NET page. What's the best, easiest way
Imagine you live in very simplified example land - and imagine that you've got
Looking at the following (simplified) hierarchy of classes: > Email (base class) > SimpleEmail
I have a database where I store objects. I have the following (simplified) schema

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.