UTF7Encoding 构造函数

定义

初始化 UTF7Encoding 类的新实例。

重载

UTF7Encoding()
已过时.

初始化 UTF7Encoding 类的新实例。

UTF7Encoding(Boolean)
已过时.

初始化 UTF7Encoding 类的新实例。 一个参数指定是否允许可选字符。

UTF7Encoding()

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs

注意

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

初始化 UTF7Encoding 类的新实例。

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 实例并显示编码的名称。

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

注解

此构造函数创建不允许可选字符的实例。 UTF7Encoding调用构造函数等效于调用UTF7Encoding.UTF7Encoding(Boolean)采用 allowOptionals 参数并为该参数指定false的构造函数。

如果实例允许可选字符,则 Unicode 码位使用相应的可选字符而不是修改后的 base 64 字符进行编码。 可选字符是感叹号 (“!”) ,向后斜杠 (“\”) ,垂直线 (“|”) ,双引号 (“”“) ,数字符号 (”#“) ,美元符号 (”$“) ,百分号 (”%“) ,与号 (”&“) ,星号 (”*“) ,分号 (”;”) ,左尖括号 (“<”) ,右尖括号 (“”>) ,左大括号 (“{”) ,右大括号 (“}”) , 左方括号 (“[”) ,右方括号 (“]”) ,等号 (“=”) ,在符号 (“@”) ,扬抑符 (“^”) ,下划线 (“_”) ,重音 (“'”) 。

注意

UTF7Encoding 不提供错误检测。 出于安全原因,建议应用程序使用 UTF8EncodingUnicodeEncodingUTF32Encoding 并启用错误检测。

适用于

UTF7Encoding(Boolean)

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs

注意

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

初始化 UTF7Encoding 类的新实例。 一个参数指定是否允许可选字符。

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,则允许指定可选字符;否则为 false

属性

示例

下面的代码示例演示如何创建允许可选字符的新 UTF7Encoding 实例。

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

注解

如果实例允许可选字符,则 Unicode 码位使用相应的可选字符而不是修改后的 base 64 字符进行编码。 可选字符是感叹号 (“!”) ,向后斜杠 (“\”) ,垂直线 (“|”) ,双引号 (“”“) ,数字符号 (”#“) ,美元符号 (”$“) ,百分号 (”%“) ,与号 (”&“) ,星号 (”*“) ,分号 (”;”) ,左尖括号 (“<”) ,右尖括号 (“”>) ,左大括号 (“{”) ,右大括号 (“}”) , 左方括号 (“[”) ,右方括号 (“]”) ,等号 (“=”) ,在符号 (“@”) ,扬抑符 (“^”) ,下划线 (“_”) ,重音 (“'”) 。

注意

UTF7Encoding 不提供错误检测。 出于安全原因,建议应用程序使用 UTF8EncodingUnicodeEncodingUTF32Encoding 并启用错误检测。

适用于