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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:43:11+00:00 2026-06-03T05:43:11+00:00

I am currently learning C# and I have a situation where I have a

  • 0

I am currently learning C# and I have a situation where I have a class that contains a ISet. I don’t wish clients to modify this set directly, and most clients only Add and Remove, and I provide accessors through my class to do this.

However, I have one client that wishes to know more about this set and its contents. I don’t really want to muddy the wrapper class itself with lots of methods for this one client, so I would prefer to be able to return the set itself in a immutable way.

I found I can’t – well, not really. The only options I seem to have are:

  1. Return an IEnumerable (No: restrictive functionality);
  2. ReadOnlyCollection (No: It’s a LIST);
  3. Return a copy (No: Bad form IMHO, allows clients to modify the returned collection perhaps unaware that it’s not going to change the real object, plus it has performance overhead);
  4. Implement my own ReadOnlySet (No: Would need to derive from ISet and thus meaning I need to implement mutators, probably firing exceptions, I would rather compile time errors – not runtime).

Am I missing something? Am I being unreasonable? Is my only option to provide the full set of accessors on my wrapper? Am I incorrect in my original intent to keep the wrapper clean for the vast majority of clients?

So two questions:

  1. Why isn’t there an standard C# immutable Collection interface? It seems like a fairly reasonable requirement?

  2. Why is ReadOnlyCollection annoyingly called ReadOnlyCollection when it is really a ReadOnlyList? I was going to bite the bullet and use that until I found out it was a List (and I use a Set).

  • 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-06-03T05:43:12+00:00Added an answer on June 3, 2026 at 5:43 am

    Why isn’t there a standard C# immutable interface? It seems like a
    fairly reasonable requirement?

    • A standard C# immutable¹ interface already exists: it’s called IEnumerable and all containers implement it.

    • More powerful immutable interfaces are problematic, because there are many kinds of immutability. If the BCL team decided to pick one definition of immutability and elevate it to the immutability status it’s certain that down the road people looking for a different kind of immutability would complain about the choice.

      Satisfying everyone would mean not only sorting all of the immutability mess out but creating lots of interfaces (good luck picking good names for them too) and baking all these immutability concepts into the language well enough to make immutability a first-class citizen — remember that there are no second chances here, once you ship a public class its public interface is immutable forever (pun intended). While all of this might be good to have, I ‘m really skeptical about the cost/benefit ratio.

    • It’s not difficult to define IReadOnlyList, IReadOnlySet and such if you do require them. I assume that they do not already exist because again, minus 100 points.

    • ReadOnlyCollection is IMHO either a concession or a class that was required internally for the BCL and exposed to the world because hey, free functionality at really low cost for the BCL team (since it would have to be implemented, documented and tested anyway). In any case I don’t think that it does not live in the glamorous System.Collections.Generic neighborhood by chance.

    Why is ReadOnlyCollection annoyingly called ReadOnlyCollection when it
    is really a ReadOnlyList? I was going to bite the bullet and use that
    until I found out it was a List (and I use a Set).

    I ‘m sure the BCL team would love to be able to go back in time and fix that, because it’s almost certainly one of those little inconsistencies that unavoidably sneak into any library of comparable scope. Since ReadOnlyCollection implements IList it should definitely have been called ReadOnlyList.

    However, given that a “list” offers more functionality than a “collection”, I don’t see how this would stop you. Neither is a Set, so you would have to build set-related functionality on top of them in any case (which is not a good idea; just build read-only semantics on top of Set).


    ¹ We ‘re tossing around “immutable” a lot here, but that word does not have a singular meaning. I think it would be more appropriate to use “read-only”, but I ‘ll go with your choice of word for consistency.

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

Sidebar

Related Questions

I am currently learning Perl. I have Perl hash that contains references to hashes
I am Currently learning Generics I have this Generic class here package generics.lesson; public
I am currently learning regex but need this quite urgently. I have a set
I don't have much experience with statically typed languages (currently learning Scala and loving
I'm currently learning about structs, so I have the following exercise: Set a Struct
I'm learning to use Grails and have run into a situation I don't understand
Currently learning Scheme/Racket and have problem running this piece of code. (if (or (<
I am currently learning jQuery and have set up a toggleSlide effect, at the
I'm currently learning MVC2, and I have seen three variants of the tags that
I'm currently learning Hibernate, and I've stumbled into this issue: I have defined 3

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.