David,
So let's make the question more concrete. In the
real world, how would you approach this problem?
Unfortunately there is no where near enough information (here) to make an
informed decision on it. Hence my statement "may hurt performance" so the OP
could weigh the options on his actual requirements and make an informed
decision. Which via profiling may need to be changed.
If it was the core function of that class (UUENCODE class) I would probably
use the ByteBuffer class I started earlier, ByteBuffer functions very
similar to the System.Text.StringBuilder class in that it maintains a byte
array internally allowing you to append bytes or byte arrays on the end,
expanding the internal array as needed.
http://groups.google.com/groups?q=B...=#[email protected]&rnum=3
I would be choosing ByteBuffer for exactly the same reason I would choose
StringBuilder or ArrayList.
Notice that the UUENCODE class itself doesn't use any of the methods you
identified per se, however the ByteBuffer class itself does (encapsulation,
reuse).
Another factor I would consider before I actually decided which method to
use first (pre-profiling) would be how large a buffer I would normally work
with, and how often it changed.
For example, if I knew I would always need a fixed size buffer (based on the
length of an input string for example) and no reallocations were needed,
then obviously this discussion is moot. Or if I knew I could use a slightly
larger buffer, with a little slack, with a single reallocation at the end,
then a simple array would do.
There may be other considerations in the OPs needs that I don't have right
now...
Again it really would depend on the complete real requirements of the
problem.
d) something else I haven't thought of
Thinking about it, I would consider using a MemoryStream also, as it
effectively is an ArrayList or StringBuilder for bytes. In other words a
dynamic array of bytes, without the boxing.
In the real world, I'd almost definitely implement originally with
boxing. I very seldom declare arraylists, so I'd be using
CollectionBase, but it's the same concept. Then I'd switch to an array
implementation only if profiling during a later cycle suggested it.
Using a CollectionBase does not feel right here, as the buffer in question
seems like an implementation detail of the UUENCODE functionality, not a
collection of domain objects per se. I normally reserve CollectionBase
classes for collections of domain objects. FWIW a Domain object is the more
general term for business object.
Unless you were suggesting you would inherit from CollectionBase to
implement a ByteBuffer class.
Of course when we get to VB.NET 2005 (Whidbey due out in 2005), Using a
generic byte list would be an option also
(
http://msdn2.microsoft.com/library/6sh2ey19.aspx).
Hope this helps
Jay