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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:29:44+00:00 2026-05-29T22:29:44+00:00

I typically try to follow a methodology of having arguments to methods be as

  • 0

I typically try to follow a methodology of having arguments to methods be as generic as possible and return types as generic as necessary and have run into a problem to which the answer is not overly clear to me.

I have a control which operates on a structure like:

IDictionary<string, IDictionary<string, IEnumerable<MyObject>>>

So that supplying two keys, we get a collection of objects back. Pretty straight forward.

I have a class

ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>

which is just that, a readonly dictionary so that any IDictionary methods which would alter the dictionary throw an NotSupportedException();

The dictionary on which the control operates comes out of a MyObjectRepository which returns a

ReadOnlyDictionary<string, ReadOnlyDictionary<string, ReadOnlyCollection<MyObject>>>

So the question is, should I update the return type on my repository to return

IDictionary<string, IDictionary<string, IEnumerable<MyObject>>>

or …? I don’t think it would be correct to update the method to take a readonly dictionary as that is unnecessarily limiting.

It seems that this makes it less clear about the return type. Also, the interface defines the ReadOnlyDictionary as the return type so that implementing classes cannot return dictionaries which would allow modification. It just feels like if the interface is returning a collection which should be read only, then the return type should reflect that. By specifying the generic IDictionary<…> return type, methods which use the interface may try to modify the dictionary only to run into a NotSupportedException.

Any suggestions on how to resolve this are appreciated!

UPDATE
As it turns out, the real issue here is with the ReadOnlyCollection. It simply should be replaced by IEnumerable and it simplifies much of the work converting between various return types.

See ReadOnlyCollection or IEnumerable for exposing member collections? and notice Jon Skeet’s answer. This is why I love SO 🙂

  • 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-29T22:29:44+00:00Added an answer on May 29, 2026 at 10:29 pm

    My inclination would be to define interfaces: DoubleKeyLookup<out ValT>, and derived from that, DoubleKeyLookup<in KeyT1, in KeyT2, out ValT>. The first would have methods like GetSequence(Object key1, Object key2);, TryGetSequence, and GetSequenceOrEmpty (the former would throw on not-found; the second would return null; the third would return Enumerable<ValT>.Empty). The second would be similar, but with ‘key’ parameters of the specified types. Most consumers would probably use the second, but the first would be usable in case one wanted to e.g. see if a particular Animal was in a dictionary where all the keys were Cat.

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

Sidebar

Related Questions

Here's a problem I run into every now and then, that I usually try
When I try to build internal DSLs in Scala, I run into a common
Typically when you run any program, during execution time what are different storages available
Typically in Django I can find out what queries are being run against the
I have developed a small application that I would like to try and sell
I am newbie with Cocoa Touch, I have a problem that I try to
I'm working with a C project that's using DirectX and I've run into a
I am having trouble with Lisp's backquote read macro. Whenever I try to write
Here is my first try at a generic histogram template function in C++ tested
I have an installer created using Visual Studio's Setup and Deployment that typically does

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.