So, you basically want to have access to elements such as:
for RowNum := 0 to csv.Count - 1 do begin Name := csv[RowNum]['Name']; // Do something end;
TObjectList<TDictionary<string, string>> probably do the job, but its not very efficient.
Loading csv into Dataset is likely to be the least code, but will have a bit more overhead.
You can consider combining a simple Tstringlist or TList<string> for the header and break the data into a new class that accepts the header in its constructor. You will get the same result:
TCSVRow = class private FHeaders: TList<string>; FFields: TList<string>; public constructor(Headers: TList<string>); function GetField(index: string): string; property Fields[index: string]: string read GetField; default; end; TCSV = class private FHeaders: TList<string>; FRows:TList<TCSVRow>; public function GetRow(Index: integer):TCSVRow; property Rows[index: integer]:TCSVRow read GetRow; default; end; implementation function TCSVRow.GetField(index: string): string; begin Result := FFields[FHeaders.IndexOf(index)]; end; function TCSV.GetRow(Index:integer):TCSVRow; begin Result := FRows[Index]; end;
This is incomplete, and I typed it directly in the browser, so I did not check it for correctness, but you got a general idea. Thus, the header information is stored only once, and is not duplicated for each row.
You can save a small bit of memory by making FFields string array instead of TList<string> , but TList<string> easier to work with IMHO.
Update David has a second point. The CSVRow class can be eliminated. You could just have either a TList<TList<string>> or a 2d array. In any case, I still think you should keep the headers on a separate list. In this case, the TCSV will be more like:
TCSV = class private FHeaders: TList<string>; FData:TList<TList<string>>; public function GetData(Row: integer; Column:string):string; property Data[Row: integer; Column:string]:string read GetData; default; end; function TCSV.GetData(Row: integer; Column:string):string; begin Result := FData[Row][FHeaders.IndexOf(Column)]; end;