C# What is the difference between Text.Encoder and Text.Encoding
Asked Answered
C

1

8

I am currently using Unicode in bytes and using Encoding class to get bytes and get strings.

However, I saw there is an encoder class and it seems like doing the same thing as the encoding class. Does anyone know what is the difference between them and when to use either of them.

Here are the Microsoft documentation page:

Encoder: https://msdn.microsoft.com/en-us/library/system.text.encoder(v=vs.110).aspx

Encoding: https://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.110).aspx

Casilde answered 12/6, 2017 at 21:17 Comment(5)
Nothing. Looks like documentation for different versions of Net Libraries, but basically the same info.Vivle
These classes have coexisted since the early days of .Net (probably since the first version) as part of the framework.Kinney
This is a really good question, and I'm surprised this hasn't been asked before. Or my Google-fu isn't strong enough.Kinney
There is some useful information on msdn.microsoft.com/en-us/library/…Dol
There is also System.Text.Decoder, important to fill the picture. They each do half of the job that Encoding does.Optic
B
9

There is definitely a difference. An Encoding is an algorithm for transforming a sequence of characters into bytes and vice versa. An Encoder is a stateful object that transforms sequences of characters into bytes. To get an Encoder object you usually call GetEncoder on an Encoding object. Why is it necessary to have a stateful tranformation? Imagine you are trying to efficiently encode long sequences of characters. You want to avoid creating a lot of arrays or one huge array. So you break the characters down into say reusable 1K character buffers. However this might make some illegal characters sequences, for example a utf-16 surrogate pair broken across to separate calls to GetBytes. The Encoder object knows how to handle this and saves the necessary state across successive calls to GetBytes. Thus you use an Encoder for transforming one block of text that is self-contained. I believe you can reuse an Encoder instance more transforms of multiple sections of text as long as you have called GetBytes with flush equal to true on the last array of characters. If you just want to easily encode short strings, use the Encoding.GetBytes methods. For the decoding operations there is a similar Decoder class that holds the decoding state.

Blasted answered 12/6, 2017 at 21:33 Comment(3)
In other words, an EncodER creates and EncodINGMagnusson
@ErikFunkenbusch other way around, an Encoding creates Encoders.Blasted
Sorry for late reply. Thanks a lot, because no one has asked this question before. Correct me if I am wrong please, "Encoding" encodes / decodes the characters in one time. "Encoder" encodes / decodes the characters by separating them into blocks, therefore it can process a much longer list of characters as a continues operation.Casilde

© 2022 - 2024 — McMap. All rights reserved.