System.Text.Encoder Class

Converts a set of characters into a sequence of bytes.

See Also: Encoder Members


public abstract class Encoder


To obtain an instance of an implementation of the System.Text.Encoder class, the application should use the Encoding.GetEncoder method of an System.Text.Encoding implementation.

The Encoder.GetByteCount(Char*, int, bool) method determines how many bytes result in encoding a set of Unicode characters, and the Encoder.GetBytes(Char*, int, Byte*, int, bool) method performs the actual encoding. There are several versions of both of these methods available in the System.Text.Encoder class. For more information, see Encoding.GetBytes(Char*, int, Byte*, int).

A System.Text.Encoder object maintains state information between successive calls to GetBytes or Decoder.Convert(Byte[], int, int, Char[], int, int, bool, Int32@, Int32@, Boolean@) methods so that it can correctly encode character sequences that span blocks. The System.Text.Encoder also preserves trailing characters at the end of data blocks and uses the trailing characters in the next encoding operation. For example, a data block might end with an unmatched high surrogate, and the matching low surrogate might be in the next data block. Therefore, Encoding.GetDecoder and Encoding.GetEncoder are useful for network transmission and file operations, because those operations often deal with blocks of data instead of a complete data stream.


When the application is done with a stream of data it should make sure that the state information is flushed by setting the flush parameter to true in the appropriate method call. If an exception occurs or if the application switches streams, it should call Encoder.Reset to clear the internal state of the Encoder object.

Version Considerations

A System.Text.Decoder or System.Text.Encoder object can be serialized during a conversion operation. The state of the object is retained if it is deserialized in the same version of the .NET Framework, but lost if it is deserialized in another version.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.


The following example demonstrates using the System.Text.UTF8Encoding class to convert one character array to two byte arrays.

C# Example

using System;
using System.Text;

public class EncoderExample

   public static void Main()

      string str = "Encoder";
      char[] cAry = str.ToCharArray();
      UTF8Encoding utf = new UTF8Encoding();
      Encoder e = utf.GetEncoder();
      int count1 = 
      int count2 = 
      byte[] bytes1 = new byte[count1];
      byte[] bytes2 = new byte[count2];

      Console.Write("Bytes1: ");
      foreach (byte b in bytes1)
         Console.Write(" '{0}' ", b);

      Console.Write("Bytes2: ");
      foreach (byte b in bytes2)
         Console.Write(" '{0}' ", b);


The output is

Bytes1: '69' '110' '99'
Bytes2: '111' '100' '101' '114'


Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0,,