StringCollection questions and sorting too

  • Thread starter Chad Z. Hower aka Kudzu
  • 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/
 
J

Jon Skeet [C# MVP]

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...
 
G

Guest

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.
 
J

Jon Skeet [C# MVP]

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.
 
C

Chad Z. Hower aka Kudzu

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/
 
C

Chad Z. Hower aka Kudzu

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/
 
C

Chad Z. Hower aka Kudzu

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/
 
J

Jon Skeet [C# MVP]

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.
 
J

Jon Skeet [C# MVP]

<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.
 

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

Similar Threads

Datagrid - Current row 6
"What is better" question ... 6
regasm and GAC accumulation. 6
Regenerating a Typed DS 2
VB.NET vs VB 8
Extending C# 4
DBGrid, CurrentRow. Easier way? 8
Formatting databound values 7

Top