Class Table
A table.
public class Table
- Inheritance
-
Table
- Inherited Members
Remarks
A table is a collection of rows, where each row is a collection of non-null string fields.
A tables can optionally contain headers, which is a collection of non-null string names that describe each column in the table.
The number of headers and the number of fields in each row do not have to match.
Tables are immutable.
Constructors
Table()
Initializes a new instance of the Table class with no headers and no rows.
public Table()
Table(IReadOnlyList<string>?, IReadOnlyList<IReadOnlyList<string>>)
Initializes a new instance of the Table class.
public Table(IReadOnlyList<string>? headers, IReadOnlyList<IReadOnlyList<string>> rows)
Parameters
headersIReadOnlyList<string>The header row, or null if there is no header row.
rowsIReadOnlyList<IReadOnlyList<string>>The list of rows.
Exceptions
- ArgumentNullException
rowsis null.
Properties
ColumnCount
Gets the column count.
public int ColumnCount { get; }
Property Value
Remarks
Each row (and the header row) in the table can have different field counts. This value will reflect the row (or header row) with the most fields. Some rows (or the header row) could have fewer fields than this value. This will return 0 if there are no headers and no rows.
Headers
Gets the header row, or null if there is no header row.
public IReadOnlyList<string>? Headers { get; }
Property Value
LooseHeaderStringComparer
Gets a string comparer that can be used to loosely compare table headers, disregarding case (per the invariant culture), white-space characters, and underscores.
public static StringComparer LooseHeaderStringComparer { get; }
Property Value
Remarks
This is effectively the same as OrdinalIgnoreCase, with the addition of ignoring white-space characters and underscores.
Rows
Gets the list of rows.
public IReadOnlyList<IReadOnlyList<string>> Rows { get; }
Property Value
Methods
EnumerateHeaderAndRows()
Enumerates all rows in the table, including the header row if one exists.
public IEnumerable<IReadOnlyList<string>> EnumerateHeaderAndRows()
Returns
- IEnumerable<IReadOnlyList<string>>
An enumerable collection of rows, starting with the header row if one exists.
FindHeaderIndex(string, StringComparer)
Finds the index of a column for a given header.
public int FindHeaderIndex(string header, StringComparer comparer)
Parameters
headerstringThe column header.
comparerStringComparerThe string comparer to use. Consider using LooseHeaderStringComparer for this.
Returns
- int
The index of the matching column, or -1 if none matched.
Exceptions
- ArgumentNullException
headeris null.- ArgumentNullException
compareris null.- InvalidOperationException
The table does not contain a header row.
- InvalidOperationException
Multiple column headers in the table match.
IsEquivalentTo(Table)
Compares this table content to another.
public bool IsEquivalentTo(Table other)
Parameters
otherTableThe other table to compare.
Returns
Exceptions
- ArgumentNullException
otheris null.
RemoveEmptyOrWhiteSpaceRowsAndColumns()
Returns a new table with all empty or white-space rows and columns removed.
public Table RemoveEmptyOrWhiteSpaceRowsAndColumns()
Returns
- Table
A new table with all empty or white-space rows and columns removed.
Remarks
For a row to be empty or white-space, every field in the row must be empty or only contain white-space characters. For a column to be empty or white-space, the field in that column in the header and in all rows must be empty or only contain white-space characters.
RemoveEmptyRowsAndColumns()
Returns a new table with all empty rows and columns removed.
public Table RemoveEmptyRowsAndColumns()
Returns
- Table
A new table with all empty rows and columns removed.
Remarks
For a row to be empty, every field in the row must be empty. For a column to be empty, the field in that column in the header and in all rows must be empty.
TryGetCellValue(IReadOnlyList<string>, string?, StringComparer, GetCellOptions, out string?)
Tries to get a cell value from a row in the table column with a given header.
public bool TryGetCellValue(IReadOnlyList<string> row, string? header, StringComparer comparer, GetCellOptions cellOptions, out string? value)
Parameters
rowIReadOnlyList<string>The row from the table.
headerstringThe optional column header. If this is null or empty, no column will be found.
comparerStringComparerThe string comparer to use. Consider using LooseHeaderStringComparer for this.
cellOptionsGetCellOptionsOptions that indicate how to interpret the cell value.
valuestringOn return, the cell value if the column and cell value were found; otherwise, null.
Returns
Remarks
The column and cell value are considered "found" when all of the following are true:
- A table column for
headeris found. rowis large enough to have an element at the table column index, and the element is not null.- After trimming white space if
cellOptionsspecifies TrimWhiteSpace, the value is not empty, unlesscellOptionsspecifies AllowEmpty.
This method does not validate that header is specified or the table column exists. Any such validation should occur separately,
before table row content is being read, as this method cannot be relied upon to perform this validation since it would never be called when a table does
not contain any rows.
row is not required to be an element of Rows.
Exceptions
- ArgumentNullException
rowis null.- ArgumentNullException
compareris null.- InvalidOperationException
The table does not contain a header row.
TryGetCellValue(IReadOnlyList<string>, string?, StringComparer, out string?)
Tries to get a cell value from a row in the table column with a given header.
public bool TryGetCellValue(IReadOnlyList<string> row, string? header, StringComparer comparer, out string? value)
Parameters
rowIReadOnlyList<string>The row from the table.
headerstringThe optional column header. If this is null or empty, no column will be found.
comparerStringComparerThe string comparer to use. Consider using LooseHeaderStringComparer for this.
valuestringOn return, the cell value if the column and cell value were found; otherwise, null.
Returns
Remarks
This is equivalent to TryGetCellValue(IReadOnlyList<string>, string?, StringComparer, GetCellOptions, out string?) with None; see the remarks for that overload for more information.
Exceptions
- ArgumentNullException
rowis null.- ArgumentNullException
compareris null.- InvalidOperationException
The table does not contain a header row.