var data = System.Text.Encoding.Default.GetBytes("你好,China");
Console.WriteLine(data.Length);
当面试官问你这打印出来是多少的时候,你一定要反问是在.net framework
下还是.net core
下。因为在.net framework
下,打印是11。在.net core
下打印是14。关于为何有此差异,MSDN
上给出了解释。
The Default property in the .NET Framework
In the .NET Framework on the Windows desktop, the Default property always gets the system‘s active code page and creates a Encoding object that corresponds to it. The active code page may be an ANSI code page, which includes the ASCII character set along with additional characters that vary by code page. Because all Default encodings based on ANSI code pages lose data, consider using the Encoding.UTF8 encoding instead. UTF-8 is often identical in the U+00 to U+7F range, but can encode characters outside the ASCII range without loss.
The Default property on .NET Core
On .NET Core, the Default property always returns the UTF8Encoding. UTF-8 is supported on all the operating systems (Windows, Linux, and Max OS X) on which .NET Core applications run.
原文地址:https://www.cnblogs.com/creekcloud/p/10240456.html