Working with CSV in C# (.NET): From Simple Parsing to Advanced Library
CSV (Comma-Separated Values) is one of the most common formats for exchanging tabular data. In the .NET ecosystem, there are several approaches to working with CSV: from built-in tools to specialized libraries. In this article, we will cover all the main methods, show code examples, and help you choose the optimal tool for your project.
1. Built-in .NET Tools (Manual Parsing)
The simplest way is to use the StreamReader and String.Split classes. However, this method does not handle escaping of quotes and commas within fields, so it is only suitable for simple CSV files without complex data.
using System;using System.IO;using System.Linq;
public class SimpleCsvReader{ public static void ReadCsv(string filePath) { using (var reader = new StreamReader(filePath)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); // Process the row Console.WriteLine(string.Join(" | ", values)); } } }}Disadvantages: does not handle quotes, does not support multiline fields, no typing.
2. Using TextFieldParser (Microsoft.VisualBasic)
The TextFieldParser class from the Microsoft.VisualBasic.FileIO namespace can correctly handle CSV with quotes and delimiters. To use it, add a reference to the Microsoft.VisualBasic assembly.
using Microsoft.VisualBasic.FileIO;using System;
public class TextFieldParserExample{ public static void ParseCsv(string filePath) { using (var parser = new TextFieldParser(filePath)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); parser.HasFieldsEnclosedInQuotes = true;
while (!parser.EndOfData) { string[] fields = parser.ReadFields(); Console.WriteLine(string.Join(" | ", fields)); } } }}This method is better, but still requires manual mapping of fields to objects.
3. CsvHelper Library — The Gold Standard
CsvHelper is the most popular library for working with CSV in .NET. It provides full support for the RFC 4180 standard, automatic mapping to classes, LINQ support, and high performance.
Installation
dotnet add package CsvHelperExample of reading CSV into a list of objects
using CsvHelper;using CsvHelper.Configuration;using System.Globalization;using System.IO;using System.Linq;
public class Person{ public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } public DateTime BirthDate { get; set; }}
public class CsvHelperReader{ public static List<Person> ReadPeople(string filePath) { using (var reader = new StreamReader(filePath)) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { return csv.GetRecords<Person>().ToList(); } }}Example of writing CSV from a collection
public static void WritePeople(string filePath, List<Person> people){ using (var writer = new StreamWriter(filePath)) using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) { csv.WriteRecords(people); }}Advanced Configuration
var config = new CsvConfiguration(CultureInfo.InvariantCulture){ HasHeaderRec Also in library
Play Services for Kotlin: Complete Guide with Examples
AutoMapper for C# (.NET): Complete Guide with Examples
Rate Limiting in Java: Libraries and Tools for Request Control (Bucket4j, Resilience4j)
Swashbuckle for C# (.NET): Complete Guide with Examples