UTF8Encoding 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 UTF8Encoding 类的新实例。
重载
| UTF8Encoding() | 初始化 UTF8Encoding 类的新实例。 | 
| UTF8Encoding(Boolean) | 初始化 UTF8Encoding 类的新实例。 参数指定是否提供一个 Unicode 字节顺序标记。 | 
| UTF8Encoding(Boolean, Boolean) | 初始化 UTF8Encoding 类的新实例。 参数指定是否提供 Unicode 字节顺序标记,以及是否在检测到无效的编码时引发异常。 | 
UTF8Encoding()
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
初始化 UTF8Encoding 类的新实例。
public:
 UTF8Encoding();public UTF8Encoding ();Public Sub New ()示例
以下示例创建一个新 UTF8Encoding 实例并显示其名称。
using namespace System;
using namespace System::Text;
int main()
{
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   String^ encodingName = utf8->EncodingName;
   Console::WriteLine( "Encoding name: {0}", encodingName );
}
using System;
using System.Text;
class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        String encodingName = utf8.EncodingName;
        Console.WriteLine("Encoding name: " + encodingName);
    }
}
Imports System.Text
Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim encodingName As String = utf8.EncodingName
        Console.WriteLine("Encoding name: " & encodingName)
    End Sub
End Class
注解
此构造函数创建一个实例,该实例不提供 Unicode 字节顺序标记,并且不会在检测到无效编码时引发异常。
注意
出于安全原因,建议通过调用具有 throwOnInvalidBytes 参数的构造函数并将其值设置为 true来启用错误检测。
另请参阅
适用于
UTF8Encoding(Boolean)
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
初始化 UTF8Encoding 类的新实例。 参数指定是否提供一个 Unicode 字节顺序标记。
public:
 UTF8Encoding(bool encoderShouldEmitUTF8Identifier);public UTF8Encoding (bool encoderShouldEmitUTF8Identifier);new System.Text.UTF8Encoding : bool -> System.Text.UTF8EncodingPublic Sub New (encoderShouldEmitUTF8Identifier As Boolean)参数
- encoderShouldEmitUTF8Identifier
- Boolean
如果为 true,则指定 GetPreamble() 方法返回 Unicode 字节顺序标记;否则为 false。
示例
以下示例创建一个新 UTF8Encoding 实例,并指定 方法应发出 GetPreamble Unicode 字节顺序标记前缀。 然后, GetPreamble 方法返回 Unicode 字节顺序标记前缀。
using namespace System;
using namespace System::Text;
using namespace System::Collections;
void ShowArray( Array^ theArray )
{
   IEnumerator^ myEnum = theArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ o = safe_cast<Object^>(myEnum->Current);
      Console::Write( "[{0}]", o );
   }
   Console::WriteLine();
}
int main()
{
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   UTF8Encoding^ utf8EmitBOM = gcnew UTF8Encoding( true );
   Console::WriteLine( "utf8 preamble:" );
   ShowArray( utf8->GetPreamble() );
   Console::WriteLine( "utf8EmitBOM:" );
   ShowArray( utf8EmitBOM->GetPreamble() );
}
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();
    }
}
Imports System.Text
Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim utf8EmitBOM As New UTF8Encoding(True)
        
        Console.WriteLine("utf8 preamble:")
        ShowArray(utf8.GetPreamble())
        
        Console.WriteLine("utf8EmitBOM:")
        ShowArray(utf8EmitBOM.GetPreamble())
    End Sub
    
    
    Public Shared Sub ShowArray(theArray As Array)
        Dim o As Object
        For Each o In  theArray
            Console.Write("[{0}]", o)
        Next o
        Console.WriteLine()
    End Sub
End Class
注解
此构造函数创建一个实例,该实例在检测到无效编码时不会引发异常。
注意
出于安全原因,应通过调用包含 throwOnInvalidBytes 参数的构造函数并将其值设置为 true来启用错误检测。
参数 encoderShouldEmitUTF8Identifier 控制 方法的操作 GetPreamble 。 如果 true为 ,则该方法返回一个字节数组,其中包含 UTF-8 格式的 Unicode 字节顺序标记 (BOM) 。  如果 false为 ,则返回零长度字节数组。 但是,将设置encoderShouldEmitUTF8Identifier到true不会导致GetBytes方法以前缀开头的字节数组,BOM 也不会导致不GetByteCount方法以包括中的 BOM 中的字节数的字节数。
另请参阅
适用于
UTF8Encoding(Boolean, Boolean)
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
- Source:
- UTF8Encoding.cs
初始化 UTF8Encoding 类的新实例。 参数指定是否提供 Unicode 字节顺序标记,以及是否在检测到无效的编码时引发异常。
public:
 UTF8Encoding(bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes);public UTF8Encoding (bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes);new System.Text.UTF8Encoding : bool * bool -> System.Text.UTF8EncodingPublic Sub New (encoderShouldEmitUTF8Identifier As Boolean, throwOnInvalidBytes As Boolean)参数
- encoderShouldEmitUTF8Identifier
- Boolean
如果为 true,则指定 GetPreamble() 方法应返回 Unicode 字节顺序标记;否则为 false。
- throwOnInvalidBytes
- Boolean
如果为 true,则在检测到无效的编码时引发异常;否则为 false。
示例
以下示例创建一个新 UTF8Encoding 实例,指定 GetPreamble 方法不应发出 Unicode 字节顺序标记前缀,并且检测到无效编码时应引发异常。 此构造函数的行为与默认 UTF8Encoding() 构造函数进行比较,后者在检测到无效编码时不会引发异常。 这两 UTF8Encoding 个实例对包含两个高代理项 (U+D801 和 U+D802) 行的字符数组进行编码,这是无效的字符序列;高代理项应始终后跟低代理项。
using namespace System;
using namespace System::Text;
void ShowArray(Array^ theArray)
{
   for each (Byte b in theArray) {
      Console::Write( "{0:X2} ", b);
   }
   Console::WriteLine();
}
int main()
{
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   UTF8Encoding^ utf8ThrowException = gcnew UTF8Encoding(false,true);
   
   // This array contains two high surrogates in a row (\uD801, \uD802).
   array<Char>^chars = {'a','b','c',L'\xD801',L'\xD802','d'};
   
   // The following method call will not throw an exception.
   array<Byte>^bytes = utf8->GetBytes( chars );
   ShowArray( bytes );
   Console::WriteLine();
   
   try {
      
      // The following method call will throw an exception.
      bytes = utf8ThrowException->GetBytes( chars );
   }
   catch (EncoderFallbackException^ e ) {
            Console::WriteLine("{0} exception\nMessage:\n{1}",
                               e->GetType()->Name, e->Message);
   }
}
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.
Imports System.Text
Class Example
    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim utf8ThrowException As New UTF8Encoding(False, True)
        
        ' Create an array with two high surrogates in a row (\uD801, \uD802).
        Dim chars() As Char = {"a"c, "b"c, "c"c, ChrW(&hD801), ChrW(&hD802), "d"c}
        
        ' The following method call will not throw an exception.
        Dim bytes As Byte() = utf8.GetBytes(chars)
        ShowArray(bytes)
        Console.WriteLine()
        
        Try
            ' The following method call will throw an exception.
            bytes = utf8ThrowException.GetBytes(chars)
            ShowArray(bytes)
        Catch e As EncoderFallbackException
            Console.WriteLine("{0} exception{2}Message:{2}{1}",
                              e.GetType().Name, e.Message, vbCrLf)
        End Try
    End Sub
    
    
    Public Shared Sub ShowArray(theArray As Array)
        For Each o In theArray
            Console.Write("{0:X2} ", o)
        Next
        Console.WriteLine()
    End Sub
End Class
' 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为 ,则返回零长度字节数组。 但是,将设置encoderShouldEmitUTF8Identifier到true不会导致GetBytes方法以前缀开头的字节数组,BOM 也不会导致不GetByteCount方法以包括中的 BOM 中的字节数的字节数。
如果 throwOnInvalidBytes 为 true,则检测无效字节序列的方法将 System.ArgumentException 引发异常。 否则,方法不会引发异常,并且将忽略无效序列。
注意
出于安全原因,应通过调用包含 throwOnInvalidBytes 参数的构造函数并将该参数设置为 来 true启用错误检测。