#1,002 – Specifying Character Encoding when Writing to a File

In .NET, string data is stored in memory as Unicode data encoded as UTF-16 (2 bytes per character, or 4 bytes for surrogate pairs).

When you persist string data out to a file, however, you must be aware of what encoding is being used. In the example below, we use a StreamWriter to write string data to a file. StreamWriter by default uses UTF-8 as the encoding.