tags:

views:

500

answers:

3

I want to build a 2 dimensional (non ragged at this point) object array.

I can easily build a 2 dimensional Array[,], and will do so if it is the best option available, but have tended to avoid arrays in favour of the advanced functionality of .NET's List and Dictionary structures.

I could also use a List<List<T>> to store a 2 dimensional array, but was wondering if there was any best-practice or implemented data structures in .NET 3.5 or above to handle Typed 2 - n dimensional structures with more flexible / comprehensive functionality than an array?

I am not interested in SSAS/OLAP style answers.

+1  A: 

I'd look at Tuple implementations for .NET 3.5. Tuples will be native in .NET 4.0, so this will be forward compatible:

Jon Galloway
I didn't know that Tuples were being properly implemented in .NET 4, something to look forward to.
johnc
+1  A: 

Arrays are one of the CLI fundamental data types. However, the "SzArray" (single dimensional, 0-based indexing) is the fastest of the arrays. It's the one you see declared int[]. Either way, if you have a fixed-size set of data, then an array (single- or multi-dimensional) will provide the best performance.

I wouldn't be at all surprised if internal optimizations made the two dimensional array faster than manually using row-major indexing for a single dimensional array. I also wouldn't be suprised if it was the other way around. Make sure to use a quality profiler if this matters heavily (either way should be fast for practical uses, I'm talking matrix math here).

280Z28
+1  A: 
  • A two dimensional array would work really well, it would consume the least amount of ram, and have the fastest lookup times. It would generally work well with read-only data
  • A list of lists, or dictionary of lists, or dictionary of arrays, or dictionary of dictionaries would work well too depending on how you need to access the data etc
Alex Black