C# – Read a text file line by line

There are two simple ways to read a text file line by line: Here’s an example of using File.ReadLines() to read a file line by line in a loop: This outputs the file’s lines: Here’s an example of using File.ReadAllLines() to read the same file into a string array and then loop through it. Skip … Read more

C# – Parsing commands and arguments in a console app

In a console app there are two ways to get commands: After getting a command, you have to parse it to figure out what code to execute. Typically commands have the following format: commandName -argumentName argumentValue. For example, take a look at this familiar git command: This is passing the command line arguments into the … Read more

C# – Reuse JsonSerializerOptions for performance

Reusing JsonSerializerOptions (from System.Text.Json) is optimal for performance. It caches type info, which results in a 200x speedup when it deals with the type again. Therefore, always try to reuse JsonSerializerOptions. I’ll show a speed comparison of serializing with and without reusing JsonSerializerOptions. Measuring the performance gains of reusing JsonSerializerOptions To measure the performance gains … Read more