CSharp - String Text Encodings and Unicode

Obtaining an Encoding

Encoding class from System.Text is the base type for classes that encapsulate text encodings.

The following code creates Encoding from encoding string.

Encoding utf8 = Encoding.GetEncoding ("utf-8");
Encoding chinese = Encoding.GetEncoding ("GB18030");

The most common encodings can also be obtained through dedicated static properties on Encoding:

Encoding name Static property on Encoding
UTF-8 Encoding.UTF8
UTF-16 Encoding.Unicode (not UTF16)
UTF-32 Encoding.UTF32
ASCII Encoding.ASCII

static GetEncodings method returns a list of all supported encodings, with their standard IANA names:

Demo

using System;
using System.Text;

class MainClass//from  w ww  .ja  v a  2  s .  com
{
    public static void Main(string[] args)
    {
        foreach (EncodingInfo info in Encoding.GetEncodings())
            Console.WriteLine(info.Name);
    }
}

Result

Encoding for file and stream I/O

The following writes "Testing..." to a file called data.txt in UTF-16 encoding:

Demo

using System;
using System.Text;

class MainClass//from   w  w w.  j av  a  2  s  . co m
{
    public static void Main(string[] args)
    {
        System.IO.File.WriteAllText("data.txt", "testing", Encoding.Unicode);
    }
}

If you omit the final argument, WriteAllText applies the ubiquitous UTF-8 encoding.

UTF-8 is the default text encoding for all file and stream I/O.