码迷,mamicode.com
首页 > Web开发 > 详细

System.Text.Encoding.Default在.net 及.netcore下不尽相同

时间:2019-01-08 19:29:05      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:use   ons   code page   asc   may   text   framework   def   desktop   

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.

System.Text.Encoding.Default在.net 及.netcore下不尽相同

标签:use   ons   code page   asc   may   text   framework   def   desktop   

原文地址:https://www.cnblogs.com/creekcloud/p/10240456.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!