Why are there no real immutable collections in C #?

I am currently learning C #, and I have a situation where I have a class containing ISet. I do not want clients to directly change this set, but most clients only add and delete, and I provide access through my class for this.

However, I have one client who wants to know more about this kit and its contents. I really don't want to mess up the wrapper class itself with a lot of methods for this single client, so I would prefer it to be able to return the set itself in an immutable way.

I found that I could not - well, actually. The only options I have are the following:

  • Returns IEnumerable (No: restrictive functionality);
  • ReadOnlyCollection (No: this is a LIST);
  • Return a copy (No: IMHO's poor form, allows clients to modify the returned collection, possibly without suspecting that it is not going to modify the real object, and also has performance overhead);
  • Implement my own ReadOnlySet (no: I would have to extract it from ISet and, therefore, I would need to implement mutators, perhaps by throwing exceptions, I would rather compile temporary errors - not runtime).

Am I missing something? Am I foolish? Is my only way to provide a complete set of accessories on my wrapper? Am I wrong in my initial intention to keep the shell clean for the vast majority of customers?

So, two questions:

  • Why is there no standard C # interface for the collection? Does this sound like a pretty reasonable requirement?

  • ReadOnlyCollection ReadOnlyCollection, ReadOnlyList? , , ( ).

+3
4

# ? , ?

  • # immutable¹: IEnumerable, .

  • , . BCL , , , , .

    , (, ) , - , , , (). , / .

  • IReadOnlyList, IReadOnlySet , . , , 100 .

  • ReadOnlyCollection , , BCL , , , BCL ( , ). , System.Collections.Generic .

ReadOnlyCollection ReadOnlyCollection, ReadOnlyList? , ( Set).

, BCL , , . ReadOnlyCollection IList, ReadOnlyList.

, , "" , "", , . Set, , ( , Set).


¹ "", . , " ", .

+8

, " " - , .

ISet, , , , , - , .

+1

, , .net , , , . promises , , , . , promises , .

, , , . ImmutableList<T>, T, , T, . T, .

druthers, , , , ( , ). IAppendable, , IImmutableEnumerable, IEnumerable ( ToImmutable IEnumerable ( IImmutableEnumerable), , . , IEnumerable, . , .

+1

All Articles