如何将编码类导入 C# 脚本

本文关键字:导入 脚本 编码 | 更新日期: 2023-09-27 18:28:04

static byte[] GetData()
{
string s = "this is some text";
byte[] data = Encoding.ASCII.GetBytes(s);
return data;
}

我发现这段代码很有用,但我遇到了 GetData(( 方法的问题。 编译代码后,我收到消息说..

错误:"编码"名称在当前上下文中不存在。

那么如何将编码类导入 C# 脚本呢?

如何将编码类导入 C# 脚本

您有两个选择:

  1. 导入脚本文件顶部的命名空间:

    using System.Text;
    
  2. 使用完全限定的类名:

    byte[] data = System.Text.Encoding.ASCII.GetBytes(s);