C# – How to create a file and write to it

There are a few ways to create a file and write to it using the .NET File API (in System.IO). The simplest way is to use high-level methods like File.WriteAllText() and File.WriteAllLines(), specifying the file path and string(s) to write to the file. Here’s an example of using these (and their async equivalents): These high-level … Read more

C# – Use yield return to minimize memory usage

Let’s say you want to search for specific characters in a large text file and return a list of context objects to the calling code for further processing (such as showing the results in the UI). One way to do that is to build the entire list at once and return it. If you don’t … 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