Last updated:
You have a CSV export from finance, a CRM or a customer, and your .NET app has to turn the rows into objects. You tried line.Split(','), it worked on the first file, and then a name like "Brown, Tom" pushed every column one place to the right.
You can read a CSV file in C# in three practical ways: StreamReader with Split (only for simple files), TextFieldParser from Microsoft.VisualBasic (built in, handles quotes), or CsvHelper (maps rows to a class). The code below was built and run on .NET 10 with CsvHelper 33.1.0 and the same APIs exist on .NET 8. The format rules are in RFC 4180.
The trap is the same in all of them: quoted fields can contain commas, quotes and even line breaks. The animation shows what a plain split does to one row.

- Which way to read a CSV file in C# should you use?
- Prerequisites
- Step 1: Create a CSV file that is hard to parse
- Step 2: Why StreamReader and Split(‘,’) break
- Step 3, option A: TextFieldParser (no package)
- Step 3, option B: CsvHelper (map rows to a class)
- What the output looks like
- Common problems and fixes
- Frequently asked questions
- Summary
Which way to read a CSV file in C# should you use?
- StreamReader + Split: no dependency, fine for files you generate yourself with no quotes, no commas inside values and no line breaks inside values. Anything from Excel or a user will eventually break it.
- TextFieldParser: ships with .NET, handles quotes and multi-line fields, reads one row at a time. You get
string[]and convert types yourself. - CsvHelper: best for real imports. Maps columns to a class, converts numbers and dates with a culture, and streams large files. For configuration depth see Read CSV files with CsvHelper in C# .NET.
Prerequisites
- A .NET 8, 9 or 10 console project.
TextFieldParserneeds no package; it is in the shared .NET runtime (Microsoft.VisualBasic.FileIO).- For CsvHelper add the package. Version 34.x changed its API (in 34.1.0 the
CsvHelper.Configurationnamespace andClassMapused below were not present), so this post pins 33.1.0:
dotnet add package CsvHelper --version 33.1.0
Step 1: Create a CSV file that is hard to parse
Save this as people.csv next to your project. It contains the cases that break parsers: a comma inside quotes, escaped quotes, an empty salary, a line break inside a value and a last row with missing columns.
Step 2: Why StreamReader and Split(‘,’) break
This is the code most tutorials start with. It runs, but look at rows 2 and 4 in the output.
foreach (var line in File.ReadLines("people.csv").Skip(1))
{
var parts = line.Split(',');
Console.WriteLine($"{parts.Length} fields: {string.Join(" | ", parts)}");
}
Row 2 became 6 fields, the quotes stayed in the data, and the multi-line row 4 was cut into two broken lines. If you use File.ReadLines (docs) or a StreamReader, the problem is the split, not the reader: a line is not always a row.
Step 3, option A: TextFieldParser (no package)
TextFieldParser understands quoted fields, doubled quotes and line breaks inside quotes. Set HasFieldsEnclosedInQuotes explicitly. It reads one row at a time, so it is fine for big files. A longer walkthrough is in Read CSV file using TextFieldParser.
using Microsoft.VisualBasic.FileIO;
using var parser = new TextFieldParser("people.csv");
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
parser.HasFieldsEnclosedInQuotes = true;
string[]? header = parser.ReadFields(); // first row
while (!parser.EndOfData)
{
string[]? f = parser.ReadFields();
if (f is null) continue;
Console.WriteLine($"{f.Length} fields: {string.Join(" | ", f)}");
}
All rows with quotes are now correct. Row 5 returns 3 fields because the file really has only 3, so check f.Length before indexing. Values are strings; parse numbers and dates yourself with CultureInfo.InvariantCulture.
Step 3, option B: CsvHelper (map rows to a class)
CsvHelper reads the header, finds the columns by name, converts types and skips nothing silently unless you tell it to. The map below also shows a header (Name) that differs from the property (FullName).
public class Person
{
public int Id { get; set; }
public string FullName { get; set; } = "";
public string? City { get; set; }
public decimal? Salary { get; set; }
public DateTime? JoinedOn { get; set; }
}
public sealed class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Map(m => m.Id).Name("Id");
Map(m => m.FullName).Name("Name"); // header name differs from the property
Map(m => m.City).Name("City");
Map(m => m.Salary).Name("Salary");
Map(m => m.JoinedOn).Name("JoinedOn");
}
}
using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
MissingFieldFound = null, // short rows leave the property at its default
BadDataFound = null,
};
using var reader = new StreamReader("people.csv");
using var csv = new CsvReader(reader, config);
csv.Context.RegisterClassMap<PersonMap>();
foreach (var p in csv.GetRecords<Person>()) // lazy: one row at a time
Console.WriteLine($"{p.Id} {p.FullName} {p.City} {p.Salary?.ToString() ?? "null"} {p.JoinedOn:yyyy-MM-dd}");
Salary is decimal?, so the empty value in row 3 becomes null. With MissingFieldFound = null, the short row 5 loads with the missing columns left empty. Without that setting CsvHelper stops on row 5 with MissingFieldException, which is usually what you want for a strict import.
Culture, delimiter and files without a header
Numbers and dates are parsed with the culture you pass. A file from Germany uses ; as delimiter, 4500,50 as decimal and 01.03.2024 as date:
var de = new CsvConfiguration(new CultureInfo("de-DE")) { Delimiter = ";" };
using var csv = new CsvReader(new StringReader("Id;Name;City;Salary;JoinedOn\n1;Jonas;Berlin;4500,50;01.03.2024\n"), de);
csv.Context.RegisterClassMap<PersonMap>();
foreach (var p in csv.GetRecords<Person>())
Console.WriteLine($"{p.Id} {p.FullName} {p.Salary} {p.JoinedOn:yyyy-MM-dd}");
For a file with no header row, turn the header off and read by index:
var cfg = new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = false };
using var csv = new CsvReader(new StringReader("1,Asha,Pune\n2,Tom,Austin\n"), cfg);
while (csv.Read())
Console.WriteLine($"{csv.GetField<int>(0)} {csv.GetField(1)} {csv.GetField(2)}");
Large files
GetRecords<T>() is lazy, so do not call ToList() on a file with millions of rows. Process each record inside the loop, or use the async version:
await foreach (var p in csv.GetRecordsAsync<Person>())
{
// handle one record, then let it go; memory stays flat
}
If you only need to move big files in pieces rather than parse them, see Read a large file in chunks in .NET Core and read a large file into a byte array with chunks.
What the output looks like
All outputs above are copied from real runs of one program on .NET 10 against the same people.csv. The comparison:
- Split: 3 of 5 rows wrong or broken.
- TextFieldParser: 5 of 5 rows correct as strings.
- CsvHelper: 5 of 5 rows correct as typed objects, with
nullfor the empty salary.
Common problems and fixes
- Columns shifted or quotes left in the values: you are using
Split(','). Switch toTextFieldParseror CsvHelper. MissingFieldExceptionorHeaderValidationExceptionin CsvHelper: a row has fewer columns than the header, or a mapped header name is not in the file. Fix the map, or setMissingFieldFound = null/HeaderValidated = nullonly if you accept partial rows.TypeConverterException: a value such as4500,50was read with the wrong culture. Pass the rightCultureInfoandDelimiter.- Strange first column name or garbled characters: pass the encoding to
StreamReader, for examplenew StreamReader(path, Encoding.UTF8); a BOM can otherwise end up in the first header name. - Out of memory on a big file: you called
ToList()or read the whole file withFile.ReadAllLines. Iterate lazily.
Frequently asked questions
What is the best way to read a CSV file in C#?
For quick, trusted files with no quotes, File.ReadLines with Split is enough. For anything that comes from users, Excel or another system, use CsvHelper (typed objects) or TextFieldParser (no package, string arrays).
Can I use TextFieldParser in .NET 8 and .NET 10?
Yes. It lives in Microsoft.VisualBasic.FileIO and is part of the shared .NET runtime, so it works in C# console and web projects without adding a package. It was run here on .NET 10.
How do I read a CSV file into a list of objects in C#?
Use CsvHelper: create a class with matching properties (or a ClassMap), open a CsvReader with the right culture, and call GetRecords<T>(). Add ToList() only when the file is small enough to hold in memory.
Summary
Do not parse CSV with Split(',') unless you control every value in the file. Use TextFieldParser when you want no dependency, and CsvHelper when you want typed objects, culture handling and streaming. Tested here on .NET 10: all samples above, including quoted commas, escaped quotes, a multi-line value, an empty value, a short row, a German-culture file, a headerless file and async streaming. Not tested: .NET 8 and 9 runs, files over a few rows, other encodings, and CsvHelper 34.x.
Found this useful?
Bookmark this page and share it with your team. Get new .NET, cloud and database tutorials in your inbox.

Thanks for all options.Did not know about Textfieldparser before .. thank you ?
Thank you for the feedback
Thanks. This article made my job easy ?.
Thanks Leon. I am glad article helped you.Thanks for the feedback!