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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:04:31+00:00 2026-05-19T06:04:31+00:00

I’d like to inject an instance an IDictionary<A, IList<B>> into a class, but want

  • 0

I’d like to inject an instance an IDictionary<A, IList<B>> into a class, but want to keep control over the implementation of IList<B> used. The way I have of doing it — with generics — feels clunky. It makes the class definition unwieldy, and would only get worse if there were multiple similar objects I wanted to inject.

public class Lookup<T1, T2, T3> where T2 : IList<T3>, new()
{
    private IDictionary<T1, T2> _underlyingDict;

    public Lookup(IDictionary<T1, T2> dict)
    {
        _underlyingDict = dict;
    }

    public T2 Get(T1 key)
    {
        return _underlyingDict[key];
    }

    public void Add(T1 key, T3 value)
    {
        if (!_underlyingDict.ContainsKey(key))
        {
            _underlyingDict[key] = new T2();
        }
        _underlyingDict[key].Add(value);
    }
}

Are there any problems with this approach, and is there a cleaner solution?

  • 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-19T06:04:32+00:00Added an answer on May 19, 2026 at 6:04 am

    That looks okay to me, if you’re happy for the caller of the constructor to still have a reference to the dictionary that your collection is based on. I would personally change the Add method to use fewer dictionary lookups though:

    public void Add(T1 key, T3 value)
    {
        T2 list;
        if (!_underlyingDict.TryGetValue(key, out list))
        {
            list = new T2();
            _underlyingDict[key] = list;
        }
        list.Add(value);
    }
    

    You might also want to consider renaming the type from Lookup given that that clashes with System.Linq.Lookup. Obviously it’s possible that you’re creating your own replacement Lookup class, which would explain this… but if there’s a chance that clients will be using both your class and “normal” LINQ, I’d consider a rename.


    Another option, which removes the need for the extra type parameter, is to accept a delegate to create a new instance of the relevant list type:

    public class Lookup<T1, T2>
    {
        private IDictionary<T1, IList<T2>> _underlyingDict;
        private Func<IList<T2>> _listCreator;
    
        public Lookup(IDictionary<T1, IList<T2>> dict,
                      Func<IList<T2>> listCreator)
        {
            _underlyingDict = dict;
            _listCreator = listCreator;
        }
    
        ...
    }
    

    then where you previously had new T2() just call _listCreator().

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

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.