public
method may return an Array
, with the exception of a byte[]
when used to represent arbitary binary data.Array
. .Arrays simply do not model any problem that I have at all well – I rarely need a collection which has the rather contradictory properties of being completely mutable, and at the same time, fixed in size. If I want to mutate a collection it is almost always to add something to it or remove something from it, not to change what value an index maps to.
We have a class or interface for everything I need. If I need a sequence I’ll use
IEnumerable<T>
, if I need a mapping from contiguous numbers to data I’ll use aList<T>
, if I need a mapping across arbitrary data I’ll use aDictionary<K,V>
, if I need a set I’ll use aHashSet<T>
. I simply don’t need arrays for anything, so I almost never use them. They don’t solve a problem I have better than the other tools at my disposal.
I make a lot of use of arrays in my protobuf-net project; entirely for performance:
- it does a lot of bit-shifting, so a
byte[]
is pretty much essential for encoding;- I use a local rolling
byte[]
buffer which I fill before sending down to the underlying stream (and v.v.); quicker thanBufferedStream
etc;- it internally uses an array-based model of objects (
Foo[]
rather thanList<Foo>
), since the size is fixed once built, and needs to be very fast.But this is definitely an exception; for general line-of-business processing, a
List<T>
wins every time.