ASP.NET Core – Add a custom InputFormatter

Input formatters are used to deserialize the request body to a model object (which is then passed into an action method). There are built-in input formatters for handling JSON and XML. You can add your own input formatter when you want to customize request body deserialization. There are two scenarios where a custom InputFormatter would … Read more

C# – Parse a comma-separated string into a list of integers

Let’s say you want to parse a comma-separated string into a list of integers. For example, you have “1,2,3” and you want to parse it into [1,2,3]. This is different from parsing CSV with rows of comma-separated values. This is more straightforward. You can use string.Split(“,”) to get the individual string values and then convert … Read more

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

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

C# – Unit testing code that does File IO

If your code does File IO, such as reading text from a file, then it’s dependent on the file system. This is an external dependency. In order to make the unit tests fast and reliable, you can mock out the external dependencies. To mock out the file system dependency, you can wrap the File IO … Read more

C# – How to unit test console output

There’s two ways to unit test code that writes to the console (Console.WriteLine() / Console.Write()): In this article, I’ll show how to do both options. Option 1 – Capture the output with Console.SetOut() Let’s say you want to unit test the following code that outputs to the console with Console.WriteLine(): You can unit test this … Read more

C# – How to unit test async methods

Let’s say you have the following async method you want to test: Here’s how to unit test this: This is awaiting the method you’re testing. To await it, you must make the unit test method return async Task. This example is a bit simplistic. In the real world when you are working with async methods, … Read more

C# – Parsing CSV data when a field has commas

When you have commas in your CSV fields, it creates a conflict with the field delimiting commas. In other words, you can’t tell which data belongs to which field. How you deal with this will depend on one question: is the field with the comma enclosed in quotes? Comma is enclosed in quotes Spreadsheet programs … Read more

C# – Parse a comma-separated string from app.config

I’ll show how to parse comma-separated integer values from app.config and load them into a HashSet for efficient lookups. First, take a look at the setting (retryStatusCodes) in app.config: To load and parse this setting from app.config, do the following: The following code shows how to do this: Note: You have to add a reference … Read more