StringCollection questions and sorting too

  • Thread starter Thread starter Chad Z. Hower aka Kudzu
  • Start date Start date
C

Chad Z. Hower aka Kudzu

StringCollection seems like a natural candidate to descend from ArrayList,
yet it descends from Object.

Why does StringCollection not descend from ArrayList? Descending from
ArrayList would give it type compatibility on methods (yes I know IList). But
it would also give it inherited functionality like Sort. StringCollection
does not have any Sort method either...

SortedList can be used, however SortedList requires keys and is a poor choice
if what I really need to store is just a list of strings.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 
Chad Z. Hower aka Kudzu said:
StringCollection seems like a natural candidate to descend from ArrayList,
yet it descends from Object.

Why does StringCollection not descend from ArrayList? Descending from
ArrayList would give it type compatibility on methods (yes I know IList). But
it would also give it inherited functionality like Sort. StringCollection
does not have any Sort method either...

SortedList can be used, however SortedList requires keys and is a poor choice
if what I really need to store is just a list of strings.

The problem is that StringCollection *isn't* an ArrayList - it would be
a bad idea to derive from it when the only thing that can be added to
it is strings. If passed an ArrayList, it's a reasonable assumption
that you can add any object to it - and that's not true of a
StringCollection.

Of course, in .NET v2 generics will solve all this...
 
It could use a few methods though.
e.x.: Sort, ToArray()
- or -
They could have derived it from ArrayList but overrided the infeface methods
of IList and any virtual methods of ArrayList.
 
Hasani (remove nospam from address) said:
It could use a few methods though.
e.x.: Sort, ToArray()
- or -
Agreed.

They could have derived it from ArrayList but overrided the infeface methods
of IList and any virtual methods of ArrayList.

That would still break Liskov's substitutability principle though - you
couldn't safely use an instance of a derived type as if it were an
instance of the base type.
 
Jon Skeet said:
The problem is that StringCollection *isn't* an ArrayList - it would be

Yes - I stated that. In fact its my contention that that is the problem. ;)
a bad idea to derive from it when the only thing that can be added to
it is strings. If passed an ArrayList, it's a reasonable assumption

Its adding type safety. Its a further specialization and is a valid reason
for descending.
that you can add any object to it - and that's not true of a
StringCollection.

Correct - but an exception can be thrown.
Of course, in .NET v2 generics will solve all this...

They will solve a lot of things - but we dont have them yet. :)


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
Hasani \(remove nospam from address\) said:
It could use a few methods though.
e.x.: Sort, ToArray()

..NET framework is pretty good - but sometimes they just blatantly overlooked
the obvious. FCL at least is decent, but WinForms is a case study...



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 
Jon Skeet said:
That would still break Liskov's substitutability principle though - you
couldn't safely use an instance of a derived type as if it were an
instance of the base type.

Why not? Strings are objects (Can be boxed into anyways). Or is that the real
difference in that StringCollection does not box them but keeps them unboxed?


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 
Chad Z. Hower aka Kudzu said:
Why not? Strings are objects (Can be boxed into anyways). Or is that the real
difference in that StringCollection does not box them but keeps them unboxed?

Strings are objects, but not all objects are strings. In other words,
it's reasonable to expect that an ArrayList *won't* throw an exception
if you try to add "new object()" to it - but that *mustn't* be allowed
for StringCollection.

By the way, you can't box a string - you can only box value types, and
string is a reference type.
 
<snip>

Chad Z. Hower aka Kudzu said:
Correct - but an exception can be thrown.

Only if you're happy to break Liskov's Substitutability Principle,
which is a bad idea, IMO.
 
Back
Top