UTF8Encoding.GetDecoder Method

Obtains a decoder that converts a UTF-8 encoded sequence of bytes into a sequence of Unicode characters.

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)

public override Decoder GetDecoder ()
public Decoder GetDecoder ()
public override function GetDecoder () : Decoder
Not applicable.

Return Value

A Decoder that converts a UTF-8 encoded sequence of bytes into a sequence of Unicode characters.

The Decoder.GetChars method converts sequential blocks of bytes into sequential blocks of characters, in a manner similar to the GetChars method of this class. However, a Decoder maintains state information between calls so it can correctly decode byte sequences that span blocks. The Decoder also preserves trailing bytes at the end of data blocks and uses the trailing bytes in the next decoding operation. Therefore, GetDecoder and GetEncoder are useful for network transmission and file operations, because those operations often deal with blocks of data instead of a complete data stream.

If error detection is enabled, that is, the throwOnInvalidCharacters parameter of the constructor is set to true, error detection is also enabled in the Decoder returned by this method. If error detection is enabled and an invalid sequence is encountered, the state of the decoder is undefined and processing must stop.

The following example demonstrates how to use the GetDecoder method to obtain a UTF-8 decoder. The decoder converts a sequence of bytes into a sequence of characters.

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
            99, 204, 128, 234, 130, 160

        Decoder utf8Decoder = Encoding.UTF8.GetDecoder();

        int charCount = utf8Decoder.GetCharCount(bytes, 0, bytes.Length);
        chars = new Char[charCount];
        int charsDecodedCount = utf8Decoder.GetChars(bytes, 0, bytes.Length, chars, 0);

            "{0} characters used to decode bytes.", charsDecodedCount

        Console.Write("Decoded chars: ");
        foreach (Char c in chars) {
            Console.Write("[{0}]", c);

import System .* ;
import System.Text .* ;

class UTF8EncodingExample
    public static void main(String[] args)
        char chars[];
        ubyte bytes[] = new ubyte[] { 99, 204, 128, 234, 130, 160 };
        Decoder utf8Decoder = Encoding.get_UTF8().GetDecoder();
        int charCount = utf8Decoder.GetCharCount(bytes, 0, bytes.length);
        chars = new char[charCount];
        int charsDecodedCount = utf8Decoder.GetChars(bytes,
            0, bytes.length, chars, 0);

        Console.WriteLine("{0} characters used to decode bytes.",
        Console.Write("Decoded chars: ");
        for (int iCtr = 0; iCtr < chars.length; iCtr++) {
            char c = chars[iCtr];
            Console.Write("[{0}]", String.valueOf(c));
    } //main
} //UTF8EncodingExample

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions