Collections that implement IList

T

Tony Johansson

Hi!

There is one property that exist in the IList interface and that is the
IsReadOnly.
It says "Gets an indicator of whether a collection can be changed."
This IsReadOnly propery also exist in IDictionary interface.

The conclusion that I make concering this IsReadOnly is that
you must is some way be able to specify that a collection can't be changed.
So if you have specified that a collection can't be changed this IsReadOnly
will return true.

So how on earth can you make a collection so that it can't be changed

//Tony
 
T

Tom Shelton

Hi!

There is one property that exist in the IList interface and that is the
IsReadOnly.
It says "Gets an indicator of whether a collection can be changed."
This IsReadOnly propery also exist in IDictionary interface.

The conclusion that I make concering this IsReadOnly is that
you must is some way be able to specify that a collection can't be changed.
So if you have specified that a collection can't be changed this IsReadOnly
will return true.

So how on earth can you make a collection so that it can't be changed

Simple - don't provide any methods to add items other then thru the
constructor. Of course, if you want it to support IList - then you would
probably want to implement add, remove, etc as explicit interface
implementations - and probably throw a NotSupportedException from the methods
that alter the list...

If you need a readonly collection, the simplest way to create a
readonly collection is to inherit from
System.Collections.ObjectModel.ReadOnlyCollection<T>. In fact, it's exactly
what I was describing above :)
 
P

Peter Duniho

Tony said:
Hi!

There is one property that exist in the IList interface and that is the
IsReadOnly.
It says "Gets an indicator of whether a collection can be changed."
This IsReadOnly propery also exist in IDictionary interface.

The conclusion that I make concering this IsReadOnly is that
you must is some way be able to specify that a collection can't be changed.
So if you have specified that a collection can't be changed this IsReadOnly
will return true.

So how on earth can you make a collection so that it can't be changed

In addition to Tom's reply, which frankly describes the only realistic,
likely scenarios, there are also contrived scenarios you could imagine.
For example:

– A collection that doesn't implement IList, but which returns an
implementation of IList on demand (e.g. via a property), and which
_becomes_ read-only upon doing so. I.e. after returning the
implementation of IList, it would then throw exceptions from methods
that could modify it.

– A collection that can be toggled, just like a file in the file
system. Sometimes it's read-only, sometimes it's not. There might even
be a private setter for the IsReadOnly property for this purpose.

– Perhaps worst example of all: a collection for which the IList
implementation (either implemented by the collection, or a separate
implementation returned from it) is read-only, but which can still be
modified from outside of the IList interface (i.e. via other members of
the original collection).

IMHO, that last example would violate at least the spirit of the
IList.IsReadOnly property documentation, if not the letter. It would
definitely be a maintenance nightmare, since I think most people would
take the IsReadOnly property value to indicate not just whether the
collection can be modified via the IList interface, but also whether the
collection is expected to change at all.

But it's certainly theoretically possible to implement, as are the other
examples (which, though somewhat more plausible than the last, are still
pretty far-fetched, I admit :) ).

Pete
 
T

Tony Johansson

Hi !

Can you provide a simple example where this IsReadOnly property wll be true
for a collection.

//Tony
 
P

Peter Duniho

Tony said:
Hi !

Can you provide a simple example where this IsReadOnly property wll be true
for a collection.

I thought Tom's reply provided plenty of information along those lines.
But, in case that wasn't enough, here's a more explicit example:

IList<int> collection =
new ReadOnlyCollection<int>(new int[] { 1, 2, 3 });

Pete
 

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

Top