Immutable Lists

  • Thread starter Thread starter phillipwei
  • Start date Start date
Hi,


| What's the best way to achieve this, or is this a bad idea?

Derive from CollectionBase and do not add a Add method. Super easy to
implement.

I do not think it's a bad idea. if you have the need for it .
 
In 2.0 there is ReadOnlyCollection<T> which does this for you.

Marc
 
Maybe better yet, derive from ReadOnlyCollectionBase and you are quite done.
 
Hi,

| Maybe better yet, derive from ReadOnlyCollectionBase and you are quite
done.

Yes, you are correct ReadOnlyCollectionBase is the way to go, Note though
that the only difference is the absence of a Clear method from the readonl
version.
 
Doesn't the CollectionBase implement the IList interface so it has "extra"
methods like Remove/RemoveAT/Insert, among the other things (so to make it
really read only, you should override those too).
The ReadOnlyCollectionBase implements only ICollection and IEnumerable so it
does not have them.
 
"Peter Bromberg [C# MVP]" <[email protected]> a écrit dans
le message de news: (e-mail address removed)...

| By Jove! Now that there is truly immutable!

....until you cast it to IList, IList<T>, ICollection or ICollection<T>, all
of which are supported by the class. :-)

Joanna
 
Supported yes, but also correctly implemented to be really
readonlycollection, for example:

void IList<T>.Insert(int index, T value) {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}

void ICollection<T>.Add(T value) {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}

So it really remains "immutable".
 
Did their homework after all.
Nice.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




Laura T. said:
Supported yes, but also correctly implemented to be really
readonlycollection, for example:

void IList<T>.Insert(int index, T value) {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}

void ICollection<T>.Add(T value) {
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}

So it really remains "immutable".

Joanna Carter said:
"Peter Bromberg [C# MVP]" <[email protected]> a écrit dans
le message de news: (e-mail address removed)...

| By Jove! Now that there is truly immutable!

...until you cast it to IList, IList<T>, ICollection or ICollection<T>,
all
of which are supported by the class. :-)

Joanna
 
"Peter Bromberg [C# MVP]" <[email protected]> a écrit dans
le message de news: (e-mail address removed)...

| Did their homework after all.
| Nice.
| Peter

| "Laura T." wrote:
|
| > Supported yes, but also correctly implemented to be really
| > readonlycollection, for example:
| >| > So it really remains "immutable".

Shucks! you saw through my little ploy :-)

Joanna
 
But ReadoOnlyCollectionBase is within the System.Collections
namespace, not System.Collections.Generic.

So if I want my immutable list to be Generic, I guess I should
implement ICollection<T>, IEnumerable<T>, ICollection, IEnumerable and
do this by hand?
 
There is a ReadOnlyCollection<T> in one of the system.collections
namespaces. I use it, but don't remember off-hand which namespace.

Mike Ober.
 
Yes, it it in the ObjectModel namespace (who knows why).
The only problem with it is that it does not lend for nice inheritance,
because ReadOnlyCollection<T> has only one constructor that needs a IList as
a parameter.. it is, afterall, ReadOnly..
 
:-) There is a reason. I had that problem years ago. I was supposed to
implement a static collection so I derived it from ReadOnlyCollectionBase.
Just a very light wrapper. But I ignored the IList interface..
After a while my collection "had learned" the capability to write too..
That's why when the generic one come out, I was eager to check if they did
the same bad thing as with ReadOnlyCollectionBase...
As Peter said, the second time, you need to do your homework properly.


Joanna Carter said:
"Peter Bromberg [C# MVP]" <[email protected]> a écrit dans
le message de news: (e-mail address removed)...

| Did their homework after all.
| Nice.
| Peter

| "Laura T." wrote:
|
| > Supported yes, but also correctly implemented to be really
| > readonlycollection, for example:
| >| > So it really remains "immutable".

Shucks! you saw through my little ploy :-)

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top