这两行有区别吗?

良朋 2021-01-25 11:06:54
这两行有区别吗?
byte[] b1 = Encoding.UTF8.GetBytes(s);
byte[] b2 = Encoding.GetEncoding("UTF-8").GetBytes(s);
...全文
265 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
晨易夕 2021-01-26
  • 打赏
  • 举报
回复
在一些特殊的时候是有区别的,比如当你那个UTF-8不是UTF-8的时候。
楠小南 2021-01-25
  • 打赏
  • 举报
回复
功能一样,实现不一样
键盘敲出字 2021-01-25
  • 打赏
  • 举报
回复
没有区别啊,实现了相同的功能啊
SoulRed 2021-01-25
  • 打赏
  • 举报
回复
这是个历史遗留问题,目前.net core正在全部统一化,结束目前版本乱七八糟的状况。
郑剑1996 2021-01-25
  • 打赏
  • 举报
回复
引用 4 楼 良朋 的回复:
[quote=引用 3 楼 灬 只有运行效率上的微妙区别,可能在根据name返回Encoding 上面,其他地方没有区别,就是一个东西
我用中文测试了一下,返回是一样的。但是三个月前我做了一个项目,用的是斑马打印机打印标签,其中有中文,当时发现第一行打印不出来中文(出来的是乱码),第二行可以打印出中文的。 当时时间紧,没细究,现在有时间想拿出来研究一下单手头没设备了,所以贴到这儿来看大家有没遇到过这种事情。[/quote] 您好,我又仔细查看了两个方式得出的结果、生成的对象、用到的dll,发现他们都是一样的。我的平台是.netcore 3.1考虑是不是平台的代码问题?
良朋 2021-01-25
  • 打赏
  • 举报
回复
[quote=引用 3 楼 灬 只有运行效率上的微妙区别,可能在根据name返回Encoding 上面,其他地方没有区别,就是一个东西[/quote] 我用中文测试了一下,返回是一样的。但是三个月前我做了一个项目,用的是斑马打印机打印标签,其中有中文,当时发现第一行打印不出来中文(出来的是乱码),第二行可以打印出中文的。 当时时间紧,没细究,现在有时间想拿出来研究一下单手头没设备了,所以贴到这儿来看大家有没遇到过这种事情。
郑剑1996 2021-01-25
  • 打赏
  • 举报
回复


    //
        // 摘要:
        //     Returns the encoding associated with the specified code page name.
        //
        // 参数:
        //   name:
        //     The code page name of the preferred encoding. Any value returned by the System.Text.Encoding.WebName
        //     property is valid. Possible values are listed in the Name column of the table
        //     that appears in the System.Text.Encoding class topic.
        //
        // 返回结果:
        //     The encoding associated with the specified code page.
        //
        // 异常:
        //   T:System.ArgumentException:
        //     name is not a valid code page name. -or- The code page indicated by name is not
        //     supported by the underlying platform.
        public static Encoding GetEncoding(string name)
        {
          Encoding ret=null;
     
       switch(name)
       {    //......
          case "UTF-8":
           ret=UTF8;
           break;
         //...
       }
     return ret;

        }


只有运行效率上的微妙区别,可能在根据name返回Encoding 上面,其他地方没有区别,就是一个东西

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧