.NET API Browser

System.Text.Decoder Class

Overview

The Decoder class converts a sequence of encoded bytes into a sequence of characters. It maintains state information between calls to handle byte sequences that span multiple blocks.

Namespace

System.Text

Assembly

System.Runtime.dll

Inheritance

Object → Decoder

Syntax

public abstract class Decoder

Members

Properties ▼

Methods ▼

Examples

// Decode a UTF‑8 byte array using a Decoder instance.
byte[] utf8Bytes = new byte[] { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0xE2, 0x98, 0x83 };
Decoder decoder = Encoding.UTF8.GetDecoder();

int charCount = decoder.GetCharCount(utf8Bytes, 0, utf8Bytes.Length);
char[] chars = new char[charCount];
decoder.GetChars(utf8Bytes, 0, utf8Bytes.Length, chars, 0);
string result = new string(chars);
Console.WriteLine(result); // Output: Hello ☃

Remarks

When decoding streams of bytes that may not be aligned on character boundaries, use a Decoder to preserve state between calls. This ensures that incomplete byte sequences are correctly handled.