While you can use Dictionary<string, Dictionary<string, T>> to do what you want, it would not be particularly memory efficient and could disrupt the synchronization of internal dictionaries. If you create your own data structure, although this is a facade for lists, using dictionaries to map column names to indexes, then this is simple enough:
public class MyDataStructure<T>//TODO come up with better name { private Dictionary<string, int> columns; private Dictionary<string, int> rows; private List<List<T>> data; public MyDataStructure( IEnumerable<string> rows, IEnumerable<string> columns) { this.columns = columns.Select((name, index) => new { name, index }) .ToDictionary(x => x.name, x => x.index); this.rows = rows.Select((name, index) => new { name, index }) .ToDictionary(x => x.name, x => x.index); initData(); } private void initData() { data = new List<List<T>>(rows.Count); for (int i = 0; i < rows.Count; i++) { data.Add(new List<T>(columns.Count)); for (int j = 0; j < columns.Count; j++) { data[i].Add(default(T)); } } } public T this[string row, string column] {
Note: if you want to fix rows / columns when building, you can use T[,] as the basis for the data, which would make the class much easier to implement, and then reduce the memory overhead, although this does not seem to work for your use cases.
Servy source share