请考虑以下代码:
DataTable myTable = loadDataTable() // loaded from SQL query in another method
int totalRecords = myTable.Rows.Count;
int recordsProcessed = 0;
Console.WriteLine("Total records: " + totalRecords;
foreach (DataRow myRow in myTable.Rows)
{
recordsProcessed++;
// do some other stuff with each row
}
Console.WriteLine("Processed records: " + recordsProcessed;
如果表格有1000行,我希望这会给我以下输出:
总记录:1000 处理记录:1000
出于某种原因,我得到了不同的结果 - 行数高达1.5倍 - 这怎么可能?我已检查并重新检查代码,以确保没有其他任何增加recordProcessed变量,这让我疯了!