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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:17:38+00:00 2026-05-12T20:17:38+00:00

I need to write a generic class where the type parameter must be something

  • 0

I need to write a generic class where the type parameter must be something implementing ICollection<T>. Inside of MyClass I need the collection’s item type (in the code snippet marked as ???).

class MyClass<TCollection> where TCollection : ICollection<???>
{
  // ...

  public void store(??? obj) { /* put obj into collection */ }

  // ...
}

Often the collection will actually be a dictionary. Sometimes, it will be something simple as a list.

I know exactly how to do this in C++. How would I do this is C#?

  • 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-12T20:17:38+00:00Added an answer on May 12, 2026 at 8:17 pm

    The simplest thing to do is just specify the element type only and hard-code ICollection<T> wherever you need it, e.g.

    class MyClass<T> {
    
        private ICollection<T> _items;
    
        public MyClass(ICollection<T> items) {
            _items = items;
        }
    
        public void Store(T obj) {
            _items.Add(obj);
        }
    
        public ICollection<T> Items {
            get {
                return _items;
            }
        }
    }
    

    I recommend that you pass in a collection instance to the constructor rather than create one internally. It makes the class simpler and more “generic” (excuse the pun), and allows you to construct collection instances with non-default constructors, e.g. a dictionary with a non-default comparator.

    RE-EDIT (3rd attempt): Using class inheritance and namespace aliasing to simulate typedef are both OK up to a point, but both abstractions break down under certain circumstances. This code is the simplest I have found that actually compiles.

    Step 1 – Define these classes:

    // This KeyValuePair was being used to simulate a tuple. We don't need to simulate a tuple when we have a concrete class.
    class BazAndListOfWrgl {
        Baz Baz { get; set; }
        List<Wrgl> Wrgls { get; set; }
    }
    
    // Simple typedef.
    class BazAndListOfWrglDictionary : Dictionary<Bar, BazAndListOfWrgl> { }
    

    Step 2 – Define these namespace aliases. All identifiers must be fully qualified. All types referenced must already be defined in a different physical file (since namespace aliases have to come before all code in a file).

    using OuterDictionary = System.Collections.Generic.Dictionary<MyNamespace.Foo, MyNamespace.BazAndListOfWrglDictionary>;
    using OuterDictionaryItem = System.Collections.Generic.KeyValuePair<MyNamespace.Foo, MyNamespace.BazAndListOfWrglDictionary>;
    

    Step 3 – Use them like this:

    class Program {
    
        static void Main() {
    
            // List-based example.
            var listWrapper = new MyClass<BazAndListOfWrgl>(new List<BazAndListOfWrgl>());
            listWrapper.Store(new BazAndListOfWrgl());
            Console.WriteLine(listWrapper.Items.Count);
    
            // Dictionary-based example.
            var dictionaryWrapper = new MyClass<OuterDictionaryItem>(new OuterDictionary());
            dictionaryWrapper.Store(new OuterDictionaryItem(new Foo(), new BazAndListOfWrglDictionary()));
            Console.WriteLine(dictionaryWrapper.Items.Count);
    
        }
    }
    

    The reasoning being: BazAndListOfWrglDictionary cannot be a namespace alias because namespace aliases cannot depend on each other. OuterDictionary and OuterDictionaryItem cannot be derived classes because otherwise the compiler does not recognise one as being the element of the other.

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

Sidebar

Related Questions

I need to write AVL-tree with generic type in C. The best way I
I need to write a generic procedure that set value for one column or
I need to write a Java Comparator class that compares Strings, however with one
I need to write code that picks up PGP-encrypted files from an FTP location
Thanks @dtb for help, he advised really need piece of code for generic service
I need to write a function that help me do something in some of
I would like to write an extension method for a generic type, with additional
why do we need a generic type specified in Boost library? Isn't template enough?
I need a way to write a generic procedure to act upon an object
Given a type T , is there any way to write something equivalent of

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.