### Auto Mapping Example Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/examples/configuration/class-maps/auto-mapping/index.md Demonstrates how to use auto mapping with a custom property name. This example shows how to automatically map properties and then override a specific property's name. Ensure the CSV file and CsvHelper are set up correctly. ```cs void Main() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { AutoMap(CultureInfo.InvariantCulture); Map(m => m.Name).Name("The Name"); } } ``` -------------------------------- ### Registering a Custom Type Converter with an Attribute Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/type-conversion/custom-type-converter/index.html This example demonstrates registering a custom type converter using an attribute on the property. This approach allows for type converter specific to a particular property. ```csharp public class Foo { [CsvConverter(typeof(MyCustomConverter))] public string Json { get; set; } } ``` -------------------------------- ### Install CsvHelper via Package Manager Console Source: https://github.com/joshclose/csvhelper/blob/master/README.markdown Use this command in the Package Manager Console to install the CsvHelper NuGet package. ```powershell PM> Install-Package CsvHelper ``` -------------------------------- ### Install CsvHelper via .NET CLI Console Source: https://github.com/joshclose/csvhelper/blob/master/README.markdown Use this command in the .NET CLI Console to add the CsvHelper NuGet package to your project. ```bash > dotnet add package CsvHelper ``` -------------------------------- ### Reading Multiple Data Sets with CsvHelper Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/reading/reading-multiple-data-sets/index.html This example shows how to read a CSV file that contains different types of records. It uses `RegisterClassMap` to define how each record type should be mapped and a `while` loop with `csv.Read()` to process records row by row. A `switch` statement on the first field of the header determines which record type to parse. ```csharp void Main() { var config = new CsvConfiguration(CultureInfo.InvariantCulture) { IgnoreBlankLines = false, }; using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, config)) { csv.Context.RegisterClassMap(); csv.Context.RegisterClassMap(); var fooRecords = new List(); var barRecords = new List(); var isHeader = true; while (csv.Read()) { if (isHeader) { csv.ReadHeader(); isHeader = false; continue; } if (string.IsNullOrEmpty(csv.GetField(0))) { isHeader = true; continue; } switch (csv.HeaderRecord[0]) { case "FooId": fooRecords.Add(csv.GetRecord()); break; case "BarId": barRecords.Add(csv.GetRecord()); break; default: throw new InvalidOperationException("Unknown record type."); } } } } public class Foo { public int Id { get; set; } public string Name { get; set; } } public class Bar { public Guid Id { get; set; } public string Name { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { Map(m => m.Id).Name("FooId"); Map(m => m.Name); } } public sealed class BarMap : ClassMap { public BarMap() { Map(m => m.Id).Name("BarId"); Map(m => m.Name); } } ``` -------------------------------- ### Write Class Objects to CSV Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/writing/write-class-objects/index.html This example shows how to write a list of custom class objects to a CSV file. Ensure you have the CsvHelper library installed. The `WriteRecords` method handles the serialization. ```csharp void Main() { var records = new List { new Foo { Id = 1, Name = "one" }, }; using (var writer = new StreamWriter("path\\to\\file.csv")) using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) { csv.WriteRecords(records); } } public class Foo { public int Id { get; set; } public string Name { get; set; } } ``` -------------------------------- ### Reading Multiple Data Sets from a Single CSV Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/examples/reading/reading-multiple-data-sets/index.md This C# example demonstrates how to read a CSV file containing multiple data sets. It uses a loop to iterate through records, detects empty rows to signify the start of a new data set, and uses a switch statement based on the header to deserialize records into the correct classes. Requires `Foo`, `Bar`, `FooMap`, and `BarMap` classes. ```csharp void Main() { var config = new CsvConfiguration(CultureInfo.InvariantCulture) { IgnoreBlankLines = false, }; using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, config)) { csv.Context.RegisterClassMap(); csv.Context.RegisterClassMap(); var fooRecords = new List(); var barRecords = new List(); var isHeader = true; while (csv.Read()) { if (isHeader) { csv.ReadHeader(); isHeader = false; continue; } if (string.IsNullOrEmpty(csv.GetField(0))) { isHeader = true; continue; } switch (csv.HeaderRecord[0]) { case "FooId": fooRecords.Add(csv.GetRecord()); break; case "BarId": barRecords.Add(csv.GetRecord()); break; default: throw new InvalidOperationException("Unknown record type."); } } } } public class Foo { public int Id { get; set; } public string Name { get; set; } } public class Bar { public Guid Id { get; set; } public string Name { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { Map(m => m.Id).Name("FooId"); Map(m => m.Name); } } public sealed class BarMap : ClassMap { public BarMap() { Map(m => m.Id).Name("BarId"); Map(m => m.Name); } } ``` -------------------------------- ### Map Class Properties to CSV Headers Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/configuration/class-maps/mapping-properties/index.html This example shows how to map properties of a C# class to CSV headers by registering a ClassMap. This is useful when your CSV headers exactly match your property names. ```csharp void Main() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { Map(m => m.Id); Map(m => m.Name); } } ``` -------------------------------- ### Reading CSV by Hand Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/examples/reading/reading-by-hand/index.md This example shows how to read a CSV file row by row without using class mapping. It iterates through each row, retrieves fields by their header name, and populates a list of custom objects. ```csharp void Main() { using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { var records = new List(); csv.Read(); csv.ReadHeader(); while (csv.Read()) { var record = new Foo { Id = csv.GetField("Id"), Name = csv.GetField("Name") }; records.Add(record); } } } public class Foo { public int Id { get; set; } public string Name { get; set; } } ``` -------------------------------- ### Auto Mapping with Custom Name Mapping Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/configuration/class-maps/auto-mapping/index.html This example shows how to use `AutoMap` to automatically map properties and then override the mapping for a specific property using `Map` to change its CSV header name. This is useful for large classes where most properties map correctly by default. ```csharp void Main() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { AutoMap(CultureInfo.InvariantCulture); Map(m => m.Name).Name("The Name"); } } ``` -------------------------------- ### Ignoring Properties with AutoMap Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/examples/configuration/class-maps/ignoring-properties/index.md This example demonstrates how to ignore a property when using AutoMap. Ensure the CSV file and Foo class properties match, and register the class map before reading records. ```cs void Main() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } public bool IsDirty { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { AutoMap(CultureInfo.InvariantCulture); Map(m => m.IsDirty).Ignore(); } } ``` -------------------------------- ### Setting a Constant Value for a Property Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/configuration/class-maps/constant-value/index.html This example shows how to use the Constant() method in a CsvHelper ClassMap to set a property to a constant value. This is useful when a property should always have the same value and does not need to be read from the CSV. ```csharp void Main() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } public bool IsDirty { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { Map(m => m.Id); Map(m => m.Name); Map(m => m.IsDirty).Constant(true); } } ``` -------------------------------- ### Mapping CSV Headers with Alternate Names Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/configuration/class-maps/mapping-by-alternate-names/index.html This example shows how to define alternate names for CSV headers within a `ClassMap`. This is useful when a header might appear with different names across various files. The `Map` method accepts multiple string arguments for alternate names. ```csharp void Main() { using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { Map(m => m.Id).Name("TheId", "Id"); Map(m => m.Name).Name("TheName", "Name"); } } ``` -------------------------------- ### Preview Documentation Locally Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/README.md Run this command to preview the documentation locally. Open your browser to the specified URL. ```bash > dotnet run -- preview --virtual-dir CsvHelper Open browser to http://localhost:5080/CsvHelper ``` -------------------------------- ### PrepareHeaderForMatchArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Illustrates the migration of PrepareHeaderForMatchArgs initialization from a constructor to an object initializer. ```csharp var args = new PrepareHeaderForMatchArgs(header, fieldIndex); ``` ```csharp var args = new PrepareHeaderForMatchArgs { Header = header, FieldIndex = fieldIndex, }; ``` -------------------------------- ### CsvConfiguration Properties Changed to Init-Only Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v20/index.html In v20, CsvConfiguration properties changed from get; set; to get; init;. This means properties can only be set during object initialization. ```csharp // v19 var config = new CsvConfiguration(CultureInfo.InvariantCulture); config.Delimiter = ";"; // v20 var config = new CsvConfiguration(CultureInfo.InvariantCulture) { Delimiter = ";", } ``` -------------------------------- ### ConvertFromStringArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Illustrates the migration of ConvertFromStringArgs initialization from constructor to object initializer. ```csharp var args = new ConvertFromStringArgs(row); ``` ```csharp var args = new ConvertFromStringArgs { Row = row, }; ``` -------------------------------- ### Custom Delimiter Configuration Source: https://github.com/joshclose/csvhelper/wiki/Custom-Configurations Example of setting a custom delimiter for CSV parsing. Instantiate CsvConfiguration and set the Delimiter property before passing it to CsvReader. ```C# FileStream f = new FileStream(filePath, FileMode.Open); StreamReader streamReader = new StreamReader(f); /* A new configuration instance that you can change at will */ CsvConfiguration config = new CsvConfiguration(); config.Delimiter = ';'; /* Give it to your CsvReader */ CsvReader csvReader = new CsvReader(streamReader, config); ``` -------------------------------- ### ReferenceHeaderPrefixArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Shows the updated way to initialize ReferenceHeaderPrefixArgs, moving from a constructor to an object initializer. ```csharp var args = new ReferenceHeaderPrefixArgs(memberType, memberName); ``` ```csharp var args = new ReferenceHeaderPrefixArgs { MemberType = memberType, MemberName = memberName, }; ``` -------------------------------- ### Configure NewLine for CSV Files Source: https://github.com/joshclose/csvhelper/blob/master/docs/getting-started/index.html Customize the newline characters used when writing CSV files. This example sets it to the environment's default newline. ```csharp var config = new CsvConfiguration(CultureInfo.InvariantCulture) { NewLine = Environment.NewLine, }; ``` -------------------------------- ### IParserConfiguration Interface Updates Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v30/index.html Classes implementing IParserConfiguration must add the MaxFixFieldSize and LeaveOpen properties. ```csharp Any class that implements `IParserConfiguration` will need to add property `double MaxFixFieldSize { get; }`. Any class that implements `IParserConfiguration` will need to add property `bool LeaveOpen { get; }`. ``` -------------------------------- ### ShouldSkipRecordArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v26/index.html Demonstrates the initialization update for ShouldSkipRecordArgs from v25 to v26. Constructor parameters are now used instead of init accessors. ```csharp // v26 var args = new ShouldSkipRecordArgs { Record = record, }; // v27 var args = new ShouldSkipRecordArgs(record); ``` -------------------------------- ### ConvertToStringArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Demonstrates the change in initializing ConvertToStringArgs from a constructor to an object initializer. ```csharp var args = new ConvertToStringArgs(value); ``` ```csharp var args = new ConvertToStringArgs { Value = value, }; ``` -------------------------------- ### Configure Highlight.js Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/csvdatareader/index.html Configures Highlight.js to use spaces for tab replacement and initializes syntax highlighting on page load. ```javascript hljs.configure({ tabReplace: " " }); hljs.initHighlightingOnLoad(); ``` -------------------------------- ### Fluent Mapping for Custom Type Converter Source: https://github.com/joshclose/csvhelper/wiki/Custom-TypeConverter Configure a custom type converter for a property using fluent mapping. This provides a programmatic way to specify the converter during CSV mapping setup. ```C# Map( m => m.MyObjectProperty ).TypeConverter(); ``` -------------------------------- ### Validate CSV Data with Class Map Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/configuration/class-maps/validation/index.html This example shows how to define a validation rule for a specific field within a CsvHelper class map. The validation ensures that the 'Name' field does not contain a hyphen. ```csharp void Main() { using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); csv.GetRecords().ToList().Dump(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } public DateTimeOffset? Date { get; set; } } public class FooMap : ClassMap { public FooMap() { Map(m => m.Id); Map(m => m.Name).Validate(args => !args.Field.Contains("-")); } } ``` -------------------------------- ### ConvertFromStringArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v26/index.html Illustrates the migration of ConvertFromStringArgs initialization from v25 to v26. Constructor parameters replace init accessors. ```csharp // v26 var args = new ConvertFromStringArgs { Row = row, }; // v27 var args = new ConvertFromStringArgs(row); ``` -------------------------------- ### Migrating ReferenceHeaderPrefixArgs Constructor Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/migration/v25/index.md Shows the migration of ReferenceHeaderPrefixArgs initialization from a constructor to an object initializer. ```cs // v25 var args = new ReferenceHeaderPrefixArgs(memberType, memberName); // v26 var args = new ReferenceHeaderPrefixArgs { MemberType = memberType, MemberName = memberName, }; ``` -------------------------------- ### Custom Type Converter for JSON - CsvHelper Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/examples/configuration/class-maps/type-conversion/index.md Use a custom type converter to deserialize JSON strings into custom objects. This example shows how to convert a JSON string to a `Json` object using `JsonConvert`. ```cs void Main() { using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); csv.GetRecords().ToList().Dump(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } public Json Json { get; set; } } public class Json { public string Foo { get; set; } } public class JsonConverter : DefaultTypeConverter { public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData) { return JsonConvert.DeserializeObject(text); } public override string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData) { return JsonConvert.SerializeObject(value); } } public class FooMap : ClassMap { public FooMap() { Map(m => m.Id); Map(m => m.Name); Map(m => m.Json).TypeConverter>(); } } ``` -------------------------------- ### ShouldQuoteArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Illustrates the migration of ShouldQuoteArgs initialization from a constructor to an object initializer. ```csharp var args = new ShouldQuoteArgs(field, fieldType, row); ``` ```csharp var args = new ShouldQuoteArgs { Field = field, FieldType = fieldType, Row = row, }; ``` -------------------------------- ### Reading CSV into Class Records Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/reading/get-class-records/index.html Reads CSV data from a file and maps each row to an instance of the 'Foo' class. Ensure the 'Foo' class properties match the CSV column headers. This example uses CultureInfo.InvariantCulture for consistent parsing. ```csharp void Main() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } } ``` -------------------------------- ### ShouldSkipRecordArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Demonstrates the change in initializing ShouldSkipRecordArgs from a constructor to an object initializer. ```csharp var args = new ShouldSkipRecordArgs(record); ``` ```csharp var args = new ShouldSkipRecordArgs { Record = record, }; ``` -------------------------------- ### ConvertToStringArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v26/index.html Demonstrates the change in initializing ConvertToStringArgs from v25 to v26. Constructor parameters are now used instead of init accessors. ```csharp // v26 var args = new ConvertToStringArgs { Value = value, }; // v27 var args = new ConvertToStringArgs(value); ``` -------------------------------- ### Ignoring a Property with AutoMap Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/configuration/class-maps/ignoring-properties/index.html This example demonstrates how to ignore a property like 'IsDirty' when using AutoMap() in a CsvHelper class map. Ensure the CSV file and the Foo class properties match, and register the class map before reading records. ```csharp void Main() { using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } public bool IsDirty { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { AutoMap(CultureInfo.InvariantCulture); Map(m => m.IsDirty).Ignore(); } } ``` -------------------------------- ### Reading Multiple Record Types with CsvHelper Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/reading/reading-multiple-record-types/index.html Configure CsvHelper to read different record types into separate lists. This example registers two class maps and uses a switch statement to determine the record type based on the first field. ```csharp void Main() { var config = new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = false, }; using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, config)) { csv.Context.RegisterClassMap(); csv.Context.RegisterClassMap(); var fooRecords = new List(); var barRecords = new List(); while (csv.Read()) { switch (csv.GetField(0)) { case "A": fooRecords.Add(csv.GetRecord()); break; case "B": barRecords.Add(csv.GetRecord()); break; default: throw new InvalidOperationException("Unknown record type."); } } } } public class Foo { public int Id { get; set; } public string Name { get; set; } } public class Bar { public Guid Id { get; set; } public string Name { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { Map(m => m.Id).Index(1); Map(m => m.Name).Index(2); } } public sealed class BarMap : ClassMap { public BarMap() { Map(m => m.Id).Index(1); Map(m => m.Name).Index(2); } } ``` -------------------------------- ### GetConstructorArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v26/index.html Shows the updated way to initialize GetConstructorArgs in v26 compared to v25. Constructor parameters are now preferred over init accessors. ```csharp // v26 var args = new GetConstructorArgs { ClassType = type, }; // v27 var args = new GetConstructorArgs(type); ``` -------------------------------- ### Reading CSV into Anonymous Type Records Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/reading/get-anonymous-type-records/index.html This example shows how to read CSV data into anonymous type objects. You need to define the anonymous type structure and then use the `GetRecords` method with that definition. Ensure you have the necessary `using` statements for `StreamReader`, `CsvReader`, and `CultureInfo`. ```csharp void Main() { using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { var anonymousTypeDefinition = new { Id = default(int), Name = string.Empty }; var records = csv.GetRecords(anonymousTypeDefinition); } } ``` -------------------------------- ### Create a List of Records Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/getting-started/index.md Prepare a collection of objects that match your class definition to be written to a CSV file. ```csharp var records = new List { new Foo { Id = 1, Name = "one" }, new Foo { Id = 2, Name = "two" }, }; ``` -------------------------------- ### GetConstructorArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Shows the updated way to initialize GetConstructorArgs, moving from a constructor to an object initializer. ```csharp var args = new GetConstructorArgs(type); ``` ```csharp var args = new GetConstructorArgs { ClassType = type, }; ``` -------------------------------- ### Open File for Writing Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/prerequisites/reading-and-writing-files/index.html Opens a file for writing using File.OpenWrite. This returns a FileStream. ```csharp using (var stream = File.OpenWrite("path\to\file.csv")) { } ``` -------------------------------- ### GetDynamicPropertyNameArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Illustrates the change in initializing GetDynamicPropertyNameArgs from a constructor to an object initializer. ```csharp var args = new GetDynamicPropertyNameArgs(index, context); ``` ```csharp var args = new GetDynamicPropertyNameArgs { FieldIndex = index, Context = context, }; ``` -------------------------------- ### ReferenceHeaderPrefixArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v26/index.html Shows the change in initializing ReferenceHeaderPrefixArgs from v25 to v26. Constructor parameters are now used instead of init accessors. ```csharp // v26 var args = new ReferenceHeaderPrefixArgs { MemberType = memberType, MemberName = memberName, }; // v27 var args = new ReferenceHeaderPrefixArgs(memberType, memberName); ``` -------------------------------- ### BadDataFoundArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Shows the change in initializing BadDataFoundArgs from a constructor call to an object initializer. ```csharp var args = new BadDataRoundArgs(field, rawRecord, context); ``` ```csharp var args = new BadDataFoundArgs { Field = field, RawRecord = rawRecord, Context = context, }; ``` -------------------------------- ### ShouldUseConstructorParametersArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Shows the updated way to initialize ShouldUseConstructorParametersArgs, moving from a constructor to an object initializer. ```csharp var args = new ShouldUseConstructorParametersArgs(parameterType); ``` ```csharp var args = new ShouldUseConstructorParametersArgs { ParameterType = parameterType, }; ``` -------------------------------- ### BadDataFoundArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v26/index.html Shows the change in initializing BadDataFoundArgs from v25 to v26. Constructor parameters are now used instead of init accessors. ```csharp // v26 var args = new BadDataFoundArgs { Field = field, RawRecord = rawRecord, Context = context, }; // v27 var args = new BadDataRoundArgs(field, rawRecord, context); ``` -------------------------------- ### Open File for Reading Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/prerequisites/reading-and-writing-files/index.html Opens a file for reading using File.OpenRead. This returns a FileStream. ```csharp using (var stream = File.OpenRead("path\to\file.csv")) { } ``` -------------------------------- ### GetDynamicPropertyNameArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v26/index.html Illustrates the initialization change for GetDynamicPropertyNameArgs between v25 and v26. Constructor parameters are now used instead of init accessors. ```csharp // v26 var args = new GetDynamicPropertyNameArgs { FieldIndex = index, Context = context, }; // v27 var args = new GetDynamicPropertyNameArgs(index, context); ``` -------------------------------- ### Mapping CSV Columns to Class Properties by Name Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/examples/configuration/class-maps/mapping-by-name/index.md Demonstrates how to configure CsvHelper to map CSV columns to class properties by name using a `ClassMap`. This is useful when column headers differ from property names. Ensure the CSV file and class definitions are set up correctly. ```csharp void Main() { using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get set; } } public sealed class FooMap : ClassMap { public FooMap() { Map(m => m.Id).Name("ColumnA"); Map(m => m.Name).Name("ColumnB"); } } ``` -------------------------------- ### Mapping Properties with Alternate Names Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/examples/configuration/class-maps/mapping-by-alternate-names/index.md Demonstrates how to map a class property to multiple possible CSV header names using `ClassMap`. This is useful when a header name might vary. Ensure the `FooMap` class is registered with the `CsvReader` context. ```cs void Main() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get set; } } public sealed class FooMap : ClassMap { public FooMap() { Map(m => m.Id).Name("TheId", "Id"); Map(m => m.Name).Name("TheName", "Name"); } } ``` -------------------------------- ### Mapping CSV by Index Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/examples/configuration/class-maps/mapping-by-index/index.md Demonstrates how to configure CsvHelper to read CSV data without a header record by mapping columns to class properties using their index. This is useful when the CSV file lacks a header row. ```cs void Main() { var config = new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = false, }; using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, config)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get set; } } public sealed class FooMap : ClassMap { public FooMap() { Map(m => m.Id).Index(0); Map(m => m.Name).Index(1); } } ``` -------------------------------- ### Write to File using StreamWriter Shortcut Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/prerequisites/reading-and-writing-files/index.html A shortcut to open a file and create a StreamWriter directly. ```csharp using (var writer = new StreamWriter("path\to\file.csv")) { } ``` -------------------------------- ### Read from File using StreamReader Shortcut Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/prerequisites/reading-and-writing-files/index.html A shortcut to open a file and create a StreamReader directly. ```csharp using (var reader = new StreamReader("path\to\file.csv")) { } ``` -------------------------------- ### Migrating ReadingExceptionOccurredArgs Constructor Source: https://github.com/joshclose/csvhelper/blob/master/src/CsvHelper.Website/input/migration/v25/index.md Demonstrates the change in initializing ReadingExceptionOccurredArgs from a constructor to an object initializer. ```cs // v25 var args = new ReadingExceptionOccurredArgs(exception); // v26 var args = new ReadingExceptionOccurredArgs { Exception = exception, }; ``` -------------------------------- ### ValidateArgs Migration Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v25/index.html Illustrates the migration of ValidateArgs initialization from a constructor to an object initializer. ```csharp var args = new ValidateArgs(field); ``` ```csharp var args = new ValidateArgs { Field = field, }; ``` -------------------------------- ### Map CSV Columns by Index Source: https://github.com/joshclose/csvhelper/blob/master/docs/examples/configuration/class-maps/mapping-by-index/index.html Demonstrates how to configure CsvHelper to read a CSV file without a header record by mapping properties to column indices. This is necessary when the CSV column order differs from the C# class property order. ```csharp void Main() { var config = new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = false, }; using (var reader = new StreamReader("path\to\file.csv")) using (var csv = new CsvReader(reader, config)) { csv.Context.RegisterClassMap(); var records = csv.GetRecords(); } } public class Foo { public int Id { get; set; } public string Name { get; set; } } public sealed class FooMap : ClassMap { public FooMap() { Map(m => m.Id).Index(0); Map(m => m.Name).Index(1); } } ``` -------------------------------- ### IWriterConfiguration Interface Updates Source: https://github.com/joshclose/csvhelper/blob/master/docs/migration/v30/index.html Classes implementing IWriterConfiguration must add the MaxFixFieldSize and LeaveOpen properties. ```csharp ixFieldSize { get; }`. Any class that implements` IWriterConfiguration`will need to add property`bool LeaveO ```