Skip to content

Read from CSV: retarget net10.0, fix the discarded configuration, add the missing data file and real tests - #2196

Merged
vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/70138-csharp-read-data-from-csv-file
Sep 18, 2026
Merged

vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/70138-csharp-read-data-from-csv-file

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates the sample behind the "How to Read Data From a CSV File in C#" article.

  • Both projects move from net6.0 to net10.0.
  • CsvHelper 27.2.1 to 33.1.0, Microsoft.NET.Test.Sdk 16.11.0 to 18.10.0, MSTest 2.2.7 to 4.4.0, coverlet.collector 3.1.0 to 10.0.1.
  • ReadPersons() built a CsvConfiguration with HasHeaderRecord = false and then handed the CsvReader a CultureInfo instead, so the configuration was discarded and the first data row was silently consumed as a header. The configuration is now passed to the reader, and the records are materialised with ToList() so the file is actually read.
  • filePersons.csv was missing from the repository, so the built console app threw FileNotFoundException before any CsvHelper code ran. It is added and copied to the output folder.
  • File-scoped namespaces, and PersonMap moves out of the global namespace into ReadFromCsv.
  • The single test asserted only that 3 == 3 and its expected data contradicted its own CSV input. It now asserts field values, with the expected data matching the input and dates parsed with InvariantCulture. New tests cover the headerless read, a quoted comma, comments with and without AllowComments, TextFieldParser against string.Split, and ReadingExceptionOccurred skipping a bad row.

Build and tests: 0 warnings, 0 errors, 7 of 7 passing on .NET 10.0.10 (SDK 10.0.302).

… the missing data file and real tests

- ReadFromCsv and ReadFromCsvTests move from net6.0 to net10.0
- CsvHelper 27.2.1 to 33.1.0, Microsoft.NET.Test.Sdk 16.11.0 to 18.10.0,
  MSTest 2.2.7 to 4.4.0, coverlet.collector 3.1.0 to 10.0.1
- ReadPersons() now passes its CsvConfiguration to the CsvReader, so
  HasHeaderRecord = false takes effect and the first data row is no longer
  consumed as a header, and materialises the records with ToList() so the
  file is actually read
- Add the missing filePersons.csv and copy it to the output folder, so the
  sample runs instead of throwing FileNotFoundException
- File-scoped namespaces, and PersonMap moves into the ReadFromCsv namespace
- Tests assert field values instead of a count that cannot fail, with the
  expected data matching the input and dates parsed with InvariantCulture,
  plus new tests for the headerless read, a quoted comma, comments with and
  without AllowComments, TextFieldParser against string.Split, and
  ReadingExceptionOccurred skipping a bad row
@vladimir-pecanac-main
vladimir-pecanac-main merged commit 0c6f3b3 into CodeMazeBlog:main Sep 18, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant