[Serializable]
public abstract class Encoding : ICloneable
{
- private static Encoding defaultEncoding;
+ // For netcore we use UTF8 as default encoding since ANSI isn't available
+ private static readonly UTF8Encoding.UTF8EncodingSealed s_defaultEncoding = new UTF8Encoding.UTF8EncodingSealed(encoderShouldEmitUTF8Identifier: false);
+
+ // Returns an encoding for the system's current ANSI code page.
+ public static Encoding Default => s_defaultEncoding;
//
// The following values are from mlang.idl. These values
return new DefaultDecoder(this);
}
- private static Encoding CreateDefaultEncoding()
- {
- // defaultEncoding should be null if we get here, but we can't
- // assert that in case another thread beat us to the initialization
-
- Encoding enc;
-
- // For netcore we use UTF8 since ANSI isn't available
- enc = new UTF8Encoding.UTF8EncodingSealed(encoderShouldEmitUTF8Identifier: false);
-
- // This method should only ever return one Encoding instance
- return Interlocked.CompareExchange(ref defaultEncoding, enc, null) ?? enc;
- }
-
- // Returns an encoding for the system's current ANSI code page.
-
- public static Encoding Default => defaultEncoding ?? CreateDefaultEncoding();
-
// Returns an Encoder object for this encoding. The returned object
// can be used to encode a sequence of characters into a sequence of bytes.
// Contrary to the GetBytes family of methods, an Encoder can
[Serializable]
internal sealed class UTF8EncodingSealed : UTF8Encoding
{
- public UTF8EncodingSealed() : base(encoderShouldEmitUTF8Identifier: true) { }
public UTF8EncodingSealed(bool encoderShouldEmitUTF8Identifier) : base(encoderShouldEmitUTF8Identifier) { }
}
// Used by Encoding.UTF8 for lazy initialization
// The initialization code will not be run until a static member of the class is referenced
- internal static readonly UTF8EncodingSealed s_default = new UTF8EncodingSealed();
+ internal static readonly UTF8EncodingSealed s_default = new UTF8EncodingSealed(encoderShouldEmitUTF8Identifier: true);
// Yes, the idea of emitting U+FEFF as a UTF-8 identifier has made it into
// the standard.