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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:00:09+00:00 2026-05-24T07:00:09+00:00

I have a generic collection that contains a method that the items in the

  • 0

I have a generic collection that contains a method that the items in the collection need to call. Each item has a reference to the generic collection. The problem is that I can’t use the type of each item as the type passed to the generic collection.

The idea is like this (I know it doesn’t compile):

    public class MyCollection<T>
    {
        public void Call(T item) { ... }
    }

    public abstract class MyItemBase
    {
        protected MyCollection<typeof(this)> _collection;
        public MyItem(MyCollection<typeof(this)> collection)
        {
            this._collection = collection;
        }

        public DoSomething()
        {
            this._collection.Call(this);
        }
    }

    public class MyItem : MyItemBase
    {
    }

Obviously, you can’t declare MyCollection<typeof(this)> but I think by writing it this way you get the general idea of what I’m trying to do. Essentially, I want the abstract class’ reference to the collection to mean MyCollection<MyClass>.

Is there any way to make this work?

  • 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-24T07:00:10+00:00Added an answer on May 24, 2026 at 7:00 am

    First of all why is your ItemBase class containing an ItemCollection? Should it not be vice-versa, where your Collection class should contain Items?

    However for your problem context, you can be type safe if you design your class this way :

     public class MyCollection<T> where T : MyItemBase<T>
    {
        public void Call(MyItemBase<T> item) { }
    }
    
    public abstract class MyItemBase<T> where T : MyItemBase<T>
    {
        protected MyCollection<T> _collection;
    
        protected MyItemBase(MyCollection<T> collection)
        {
            _collection = collection;
        }
    
        public void DoSomething()
        {
            _collection.Call(this);
        }
    }
    
    public class MyItem : MyItemBase<MyItem>
    {
        public MyItem(MyCollection<MyItem> Collection)
            : base(Collection)
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface that contains a single method called ListAll() using System.Collections.Generic; namespace
Background: I have a generic collection of type DirectoryCollection<T> and have specified that T
I have a generic Collection and am trying to work out how I can
I have a generic business object collection class which contains some business objects: public
I have a generic class that has one type parameter (T). I needed to
I have a system.collections.generic.list(of ListBox) I would like to use the collection classes built-in
LinQ contains the method Cast which casts each entry in the list to type
I'm having a problem that I have encountered before, but I still don't know
I have an abstract class foo that contains some properties and methods. I have
I have a list that contains FrameworkElements and I want to create an extension

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.