UTF8Encoding 构造函数

定义

初始化 UTF8Encoding 类的新实例。

重载

UTF8Encoding()

初始化 UTF8Encoding 类的新实例。

UTF8Encoding(Boolean)

初始化 UTF8Encoding 类的新实例。 参数指定是否提供一个 Unicode 字节顺序标记。

UTF8Encoding(Boolean, Boolean)

初始化 UTF8Encoding 类的新实例。 参数指定是否提供 Unicode 字节顺序标记,以及是否在检测到无效的编码时引发异常。

UTF8Encoding()

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs

初始化 UTF8Encoding 类的新实例。

C#
public UTF8Encoding ();

示例

以下示例创建一个新 UTF8Encoding 实例并显示其名称。

C#
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        String encodingName = utf8.EncodingName;
        Console.WriteLine("Encoding name: " + encodingName);
    }
}

注解

此构造函数创建一个实例,该实例不提供 Unicode 字节顺序标记,并且不会在检测到无效编码时引发异常。

注意

出于安全原因,建议通过调用具有 throwOnInvalidBytes 参数的构造函数并将其值设置为 true来启用错误检测。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

UTF8Encoding(Boolean)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs

初始化 UTF8Encoding 类的新实例。 参数指定是否提供一个 Unicode 字节顺序标记。

C#
public UTF8Encoding (bool encoderShouldEmitUTF8Identifier);

参数

encoderShouldEmitUTF8Identifier
Boolean

如果为 true,则指定 GetPreamble() 方法返回 Unicode 字节顺序标记;否则为 false

示例

以下示例创建一个新 UTF8Encoding 实例,并指定 方法应发出 GetPreamble Unicode 字节顺序标记前缀。 然后, GetPreamble 方法返回 Unicode 字节顺序标记前缀。

C#
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        UTF8Encoding utf8EmitBOM = new UTF8Encoding(true);

        Console.WriteLine("utf8 preamble:");
        ShowArray(utf8.GetPreamble());

        Console.WriteLine("utf8EmitBOM:");
        ShowArray(utf8EmitBOM.GetPreamble());
    }

    public static void ShowArray(Array theArray) {
        foreach (Object o in theArray) {
            Console.Write("[{0}]", o);
        }
        Console.WriteLine();
    }
}

注解

此构造函数创建一个实例,该实例在检测到无效编码时不会引发异常。

注意

出于安全原因,应通过调用包含 throwOnInvalidBytes 参数的构造函数并将其值设置为 true来启用错误检测。

参数 encoderShouldEmitUTF8Identifier 控制 方法的操作 GetPreamble 。 如果 true为 ,则该方法返回一个字节数组,其中包含 UTF-8 格式的 Unicode 字节顺序标记 (BOM) 。 如果 false为 ,则返回零长度字节数组。 但是,将设置encoderShouldEmitUTF8Identifiertrue不会导致GetBytes方法以前缀开头的字节数组,BOM 也不会导致不GetByteCount方法以包括中的 BOM 中的字节数的字节数。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

UTF8Encoding(Boolean, Boolean)

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs

初始化 UTF8Encoding 类的新实例。 参数指定是否提供 Unicode 字节顺序标记,以及是否在检测到无效的编码时引发异常。

C#
public UTF8Encoding (bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes);

参数

encoderShouldEmitUTF8Identifier
Boolean

如果为 true,则指定 GetPreamble() 方法应返回 Unicode 字节顺序标记;否则为 false

throwOnInvalidBytes
Boolean

如果为 true,则在检测到无效的编码时引发异常;否则为 false

示例

以下示例创建一个新 UTF8Encoding 实例,指定 GetPreamble 方法不应发出 Unicode 字节顺序标记前缀,并且检测到无效编码时应引发异常。 此构造函数的行为与默认 UTF8Encoding() 构造函数进行比较,后者在检测到无效编码时不会引发异常。 这两 UTF8Encoding 个实例对包含两个高代理项 (U+D801 和 U+D802) 行的字符数组进行编码,这是无效的字符序列;高代理项应始终后跟低代理项。

C#
using System;
using System.Text;

class Example
{
    public static void Main()
    {
        UTF8Encoding utf8 = new UTF8Encoding();
        UTF8Encoding utf8ThrowException = new UTF8Encoding(false, true);

        // Create an array with two high surrogates in a row (\uD801, \uD802).
        Char[] chars = new Char[] {'a', 'b', 'c', '\uD801', '\uD802', 'd'};

        // The following method call will not throw an exception.
        Byte[] bytes = utf8.GetBytes(chars);
        ShowArray(bytes);
        Console.WriteLine();

        try {
            // The following method call will throw an exception.
            bytes = utf8ThrowException.GetBytes(chars);
            ShowArray(bytes);
        }
        catch (EncoderFallbackException e) {
            Console.WriteLine("{0} exception\nMessage:\n{1}",
                              e.GetType().Name, e.Message);
        }
    }

    public static void ShowArray(Array theArray) {
        foreach (Object o in theArray)
            Console.Write("{0:X2} ", o);

        Console.WriteLine();
    }
}
// The example displays the following output:
//    61 62 63 EF BF BD EF BF BD 64
//
//    EncoderFallbackException exception
//    Message:
//    Unable to translate Unicode character \uD801 at index 3 to specified code page.

注解

参数 encoderShouldEmitUTF8Identifier 控制 方法的操作 GetPreamble 。 如果 true为 ,则该方法返回一个字节数组,其中包含 UTF-8 格式的 Unicode 字节顺序标记 (BOM) 。 如果 false为 ,则返回零长度字节数组。 但是,将设置encoderShouldEmitUTF8Identifiertrue不会导致GetBytes方法以前缀开头的字节数组,BOM 也不会导致不GetByteCount方法以包括中的 BOM 中的字节数的字节数。

如果 throwOnInvalidBytestrue,则检测无效字节序列的方法将 System.ArgumentException 引发异常。 否则,方法不会引发异常,并且将忽略无效序列。

注意

出于安全原因,应通过调用包含 throwOnInvalidBytes 参数的构造函数并将该参数设置为 来 true启用错误检测。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0