C# – Using CsvHelper when there’s no header row

When you’re parsing CSV with CsvHelper and there’s no header row, you have to configure it to map by field position. I’ll show how to do that. At the end, I’ll show the alternative approach of manually parsing in this scenario. Consider the following CSV data without a header row: Normally, CsvHelper maps fields to … Read more

C# – Parsing a CSV file

In this article, I’ll show how to parse a CSV file manually and with a parser library (CsvHelper). Let’s say you have the following CSV file: To manually parse this, read the file line by line and split each line with a comma. This gives you a string array containing the fields that you can … Read more

HackerRank – Zig Zag Sequence solution

In this article, I’ll explain the Zig Zag Sequence algorithm problem on HackerRank. Problem statement: You’re given an integer array with an odd number of elements (ex: [5, 2, 3, 1, 4]). You need to re-arrange the elements so they’re in a zig zag sequence, which means: Here’s a diagram to help you visualize what … Read more

C# – Using top-level statements

The top-level statements feature makes the Main() method implicit. This feature was added in C# 9 (.NET 5) with the purpose of decluttering a project’s entry point. Here’s what a console app looks like using a top-level statement: The compiler generates the Main() method implicitly from the top-level statements. The code above is functionally equivalent … Read more

C# – Save a list of strings to a file

The simplest way to save a list of strings to a file is to use File.WriteAllLines(). This creates (or overwrites) the file and writes each string on a new line. The resulting file looks like this: Note: Showing non-printable newline characters \r\n for clarity. Specifying the separator character What if you want to separate each … Read more

Multithreaded quicksort in C#

One day I decided to challenge myself by trying to implement multithreaded quicksort. I wanted to see how it would compare to the built-in Array.Sort() method. I came up with two algorithms that were 2-4x faster than Array.Sort(): After continuing to tinker, in attempts to further optimize, I came across the AsParallel().OrderBy() method (PLINQ). After … Read more