Thread safety and System.Text.Encoding in C#

前端 未结 1 1017
野性不改
野性不改 2021-01-17 08:09

Is it safe to use the same Encoding object from different threads?

By \"using\" I mean, calling Encoding.GetString(), Encoding.GetByt

相关标签:
1条回答
  • 2021-01-17 08:46

    Yes, it should be safe to use the same Encoding object, as it's designed to be stateless - whereas Encoder and Decoder are stateful, maintaining incomplete characters etc if necessary. I suppose you could write a stateful Encoding class, but it would be a really bad idea. As far as I'm aware, all of the built-in encoding implementations are stateless and thread-safe.

    For example, the Encoding.UTF8, Encoding.ASCII etc properties are singletons.

    0 讨论(0)
提交回复
热议问题