Your check doesn't make sense and also forgets one important.
DataSet.Tables
also can't be null
because it's a readonly property, you can't assign null
, so the second check is pointless.
dataSet.Tables[0].Rows
can't be null
because it's a readonly property, you can't assign null
, so the last check is redundant.
But you forgot that the DataSet
could be empty, so doesn't contain any DataTables
. In that case your if
throws an exception at dataSet.Tables[0]
.
I would use:
int? firstTablesRowCount = dataSet?.Tables.Cast<DataTable>().FirstOrDefault()?.Rows.Count;
if (firstTablesRowCount.GetValueOrDefault() == 0)
{
Console.WriteLine($"Error at {nameof(dataSet)}");
}
This ensures that the DataSet
isn't null and contains tables and that the first table contains rows.