UTF7Encoding 构造函数

定义

初始化 UTF7Encoding 类的新实例。Initializes a new instance of the UTF7Encoding class.

重载

UTF7Encoding()
已过时。

初始化 UTF7Encoding 类的新实例。Initializes a new instance of the UTF7Encoding class.

UTF7Encoding(Boolean)
已过时。

初始化 UTF7Encoding 类的新实例。Initializes a new instance of the UTF7Encoding class. 一个参数指定是否允许可选字符。A parameter specifies whether to allow optional characters.

UTF7Encoding()

注意

The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.

初始化 UTF7Encoding 类的新实例。Initializes a new instance of the UTF7Encoding class.

public:
 UTF7Encoding();
public UTF7Encoding ();
[System.Obsolete("The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.", DiagnosticId="SYSLIB0001", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public UTF7Encoding ();
Public Sub New ()
属性

示例

下面的代码示例演示如何创建新的 UTF7Encoding 实例并显示编码的名称。The following code example demonstrates how to create a new UTF7Encoding instance and display the name of the encoding.

using namespace System;
using namespace System::Text;
int main()
{
   UTF7Encoding^ utf7 = gcnew UTF7Encoding;
   String^ encodingName = utf7->EncodingName;
   Console::WriteLine( "Encoding name: {0}", encodingName );
}

using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        UTF7Encoding utf7 = new UTF7Encoding();
        String encodingName = utf7.EncodingName;
        Console.WriteLine("Encoding name: " + encodingName);
    }
}
Imports System.Text

Class UTF7EncodingExample
    
    Public Shared Sub Main()
        Dim utf7 As New UTF7Encoding()
        Dim encodingName As String = utf7.EncodingName
        Console.WriteLine("Encoding name: " & encodingName)
    End Sub
End Class

注解

此构造函数将创建一个不允许使用可选字符的实例。This constructor creates an instance that does not allow optional characters. 调用 UTF7Encoding 构造函数等效于调用 UTF7Encoding.UTF7Encoding(Boolean) 采用 allowOptionals 参数并为该参数指定的构造函数 falseCalling the UTF7Encoding constructor is equivalent to calling the UTF7Encoding.UTF7Encoding(Boolean) constructor that takes an allowOptionals parameter and specifying false for that parameter.

如果实例允许使用可选字符,则使用相应的可选字符(而不是修改后的 base 64 字符)对 Unicode 码位进行编码。If an instance allows optional characters, Unicode code points are encoded with a corresponding optional character instead of a modified base 64 character. 可选字符为惊叹号 ( "!") 、反斜杠 ( " \ " ) 、竖线 ( "|" ) 、双引号 ( "" ") 、数字符号 (" # ") 、美元符号 (" $ ") 、百分比符号 ("% ") 、and (" & ") 、星号 (" * ") 、分号 ("; ") , ( "" 的左尖括号 <"), right angle bracket ("> ) ,左大括号 ( "{" ) ,右大括号 ( "}" ) ,左方括号 ( "[" ) ,右方括号 ( "]" ) ,等号 ( "=" ) ,at 符号 ( "@" ) ,重音符号 ( "^" ) ,下划线 ( "" ) ,以及抑音符 ( "' ) "。The optional characters are exclamation point ("!"), backward slash ("\"), vertical line ("|"), double quote ("""), number sign ("#"), dollar sign ("$"), percent sign ("%"), ampersand ("&"), asterisk ("*"), semicolon (";"), left angle bracket ("<"), right angle bracket (">"), left curly bracket ("{"), right curly bracket ("}"), left square bracket ("["), right square bracket ("]"), equal sign ("="), at sign ("@"), circumflex accent ("^"), underscore (""), and grave accent ("`").

备注

UTF7Encoding 不提供错误检测。UTF7Encoding does not provide error detection. 出于安全原因,建议您的应用程序使用 UTF8EncodingUnicodeEncodingUTF32Encoding 并启用错误检测。For security reasons, your applications are recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

适用于

UTF7Encoding(Boolean)

注意

The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.

初始化 UTF7Encoding 类的新实例。Initializes a new instance of the UTF7Encoding class. 一个参数指定是否允许可选字符。A parameter specifies whether to allow optional characters.

public:
 UTF7Encoding(bool allowOptionals);
public UTF7Encoding (bool allowOptionals);
[System.Obsolete("The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.", DiagnosticId="SYSLIB0001", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public UTF7Encoding (bool allowOptionals);
new System.Text.UTF7Encoding : bool -> System.Text.UTF7Encoding
[<System.Obsolete("The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.", DiagnosticId="SYSLIB0001", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Text.UTF7Encoding : bool -> System.Text.UTF7Encoding
Public Sub New (allowOptionals As Boolean)

参数

allowOptionals
Boolean

如果为 true,则允许指定可选字符;否则为 falsetrue to specify that optional characters are allowed; otherwise, false.

属性

示例

下面的代码示例演示如何创建一个允许使用 UTF7Encoding 可选字符的新实例。The following code example demonstrates how to create a new UTF7Encoding instance that allows optional characters.

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()
{
   
   // A few optional characters.
   String^ chars = "!@#$";
   
   // The default Encoding does not allow optional characters.
   // Alternate Byte values are used.
   UTF7Encoding^ utf7 = gcnew UTF7Encoding;
   array<Byte>^bytes1 = utf7->GetBytes( chars );
   Console::WriteLine( "Default UTF7 Encoding:" );
   ShowArray( bytes1 );
   
   // Convert back to characters.
   Console::WriteLine( "Characters:" );
   ShowArray( utf7->GetChars( bytes1 ) );
   
   // Now, allow optional characters.
   // Optional characters are encoded with their normal code points.
   UTF7Encoding^ utf7AllowOptionals = gcnew UTF7Encoding( true );
   array<Byte>^bytes2 = utf7AllowOptionals->GetBytes( chars );
   Console::WriteLine( "UTF7 Encoding with optional characters allowed:" );
   ShowArray( bytes2 );
   
   // Convert back to characters.
   Console::WriteLine( "Characters:" );
   ShowArray( utf7AllowOptionals->GetChars( bytes2 ) );
}

using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {

        // A few optional characters.
        string chars = "!@#$";

        // The default Encoding does not allow optional characters.
        // Alternate byte values are used.
        UTF7Encoding utf7 = new UTF7Encoding();
        Byte[] bytes1 = utf7.GetBytes(chars);
        
        Console.WriteLine("Default UTF7 Encoding:");
        ShowArray(bytes1);

        // Convert back to characters.
        Console.WriteLine("Characters:");
        ShowArray(utf7.GetChars(bytes1));

        // Now, allow optional characters.
        // Optional characters are encoded with their normal code points.
        UTF7Encoding utf7AllowOptionals = new UTF7Encoding(true);
        Byte[] bytes2 = utf7AllowOptionals.GetBytes(chars);
        
        Console.WriteLine("UTF7 Encoding with optional characters allowed:");
        ShowArray(bytes2);

        // Convert back to characters.
        Console.WriteLine("Characters:");
        ShowArray(utf7AllowOptionals.GetChars(bytes2));
    }

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

Imports System.Text

Class UTF7EncodingExample
    
    Public Shared Sub Main()
        
        ' A few optional characters.
        Dim chars As String = "!@#$"
        
        ' The default Encoding does not allow optional characters.
        ' Alternate byte values are used.
        Dim utf7 As New UTF7Encoding()
        Dim bytes1 As Byte() = utf7.GetBytes(chars)
        
        Console.WriteLine("Default UTF7 Encoding:")
        ShowArray(bytes1)
        
        ' Convert back to characters.
        Console.WriteLine("Characters:")
        ShowArray(utf7.GetChars(bytes1))
        
        ' Now, allow optional characters.
        ' Optional characters are encoded with their normal code points.
        Dim utf7AllowOptionals As New UTF7Encoding(True)
        Dim bytes2 As Byte() = utf7AllowOptionals.GetBytes(chars)
        
        Console.WriteLine("UTF7 Encoding with optional characters allowed:")
        ShowArray(bytes2)
        
        ' Convert back to characters.
        Console.WriteLine("Characters:")
        ShowArray(utf7AllowOptionals.GetChars(bytes2))
    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

注解

如果实例允许使用可选字符,则使用相应的可选字符(而不是修改后的 base 64 字符)对 Unicode 码位进行编码。If an instance allows optional characters, Unicode code points are encoded with a corresponding optional character instead of a modified base 64 character. 可选字符为惊叹号 ( "!") 、反斜杠 ( " \ " ) 、竖线 ( "|" ) 、双引号 ( "" ") 、数字符号 (" # ") 、美元符号 (" $ ") 、百分比符号 ("% ") 、and (" & ") 、星号 (" * ") 、分号 ("; ") , ( "" 的左尖括号 <"), right angle bracket ("> ) ,左大括号 ( "{" ) ,右大括号 ( "}" ) ,左方括号 ( "[" ) ,右方括号 ( "]" ) ,等号 ( "=" ) ,at 符号 ( "@" ) ,重音符号 ( "^" ) ,下划线 ( "" ) ,以及抑音符 ( "' ) "。The optional characters are exclamation point ("!"), backward slash ("\"), vertical line ("|"), double quote ("""), number sign ("#"), dollar sign ("$"), percent sign ("%"), ampersand ("&"), asterisk ("*"), semicolon (";"), left angle bracket ("<"), right angle bracket (">"), left curly bracket ("{"), right curly bracket ("}"), left square bracket ("["), right square bracket ("]"), equal sign ("="), at sign ("@"), circumflex accent ("^"), underscore (""), and grave accent ("`").

备注

UTF7Encoding 不提供错误检测。UTF7Encoding does not provide error detection. 出于安全原因,建议您的应用程序使用 UTF8EncodingUnicodeEncodingUTF32Encoding 并启用错误检测。For security reasons, your applications are recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

适用于