Encoding.Default Property

Definition

Gets the default encoding for this .NET implementation.

public:
 static property System::Text::Encoding ^ Default { System::Text::Encoding ^ get(); };
public static System.Text.Encoding Default { get; }
static member Default : System.Text.Encoding
Public Shared ReadOnly Property Default As Encoding

Property Value

The default encoding for this .NET implementation.

Remarks

The behavior of the Default property varies between different .NET implementations:

  • In .NET Framework: Returns the encoding that corresponds to the system's active code page. This is the same encoding returned by GetEncoding(Int32) when called with a codepage argument of 0.

  • In .NET Core and later versions: Always returns a UTF8Encoding object. This behavior was changed to encourage the use of Unicode encodings for better cross-platform compatibility and data integrity.

For the most consistent results across different platforms and .NET implementations, consider using a specific Unicode encoding such as UTF-8 directly instead of relying on the default encoding. You can obtain UTF-8 encoding by calling Encoding.UTF8 or Encoding.GetEncoding(String) with "utf-8".

Warning

Different computers can use different encodings as the default, and the default encoding can change on a single computer. If you use the Encoding.Default encoding to encode and decode data streamed between computers or retrieved at different times on the same computer, it might translate that data incorrectly. In addition, the encoding returned by the Default property uses best-fit fallback to map unsupported characters to characters supported by the code page. For these reasons, using the default encoding is not recommended. To ensure that encoded bytes are decoded properly, you should use a Unicode encoding, such as UTF8Encoding or UnicodeEncoding. You could also use a higher-level protocol to ensure that the same format is used for encoding and decoding.

The Default property always returns the UTF8Encoding. UTF-8 is supported on all the operating systems (Windows, Linux, and macOS) on which .NET apps run.

Applies to