UTF7Encoding 建構函式

定義

初始化 UTF7Encoding 類別的新執行個體。

多載

UTF7Encoding()
已淘汰.

初始化 UTF7Encoding 類別的新執行個體。

UTF7Encoding(Boolean)
已淘汰.

初始化 UTF7Encoding 類別的新執行個體。 參數可指定是否允許使用選擇性字元。

UTF7Encoding()

來源:
UTF7Encoding.cs
來源:
UTF7Encoding.cs
來源:
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 字碼點會以對應的選擇性字元編碼,而不是修改的基底 64 字元。 選擇性字元是驚嘆號 (“!”) ,回斜線 (“\”) ,垂直線 (“|”) ,雙引號 (“”) ,數位符號 (“#”) ,貨幣符號 (“$”) ,百分比符號 (“%”) “,ampersand (”&“) ,星號 (”*“) ,分號 (”;”) ,左角括弧 (“<”) ,右角括弧 (“>”“) ,左大括弧 (”{“) ,右大括弧 (”}“) , 左方括弧 (”[“) ,右方括弧 (”]“) ,等號 (”=“) ,at sign (”@“) ,圓角輔色 (”^“) ,底線 (”_“) ,以及 (”'“) 。

注意

UTF7Encoding 不提供錯誤偵測。 基於安全性考慮,建議您使用 UTF8EncodingUnicodeEncodingUTF32Encoding ,並啟用錯誤偵測。

適用於

UTF7Encoding(Boolean)

來源:
UTF7Encoding.cs
來源:
UTF7Encoding.cs
來源:
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 字碼點會以對應的選擇性字元編碼,而不是修改的基底 64 字元。 選擇性字元是驚嘆號 (“!”) ,回斜線 (“\”) ,垂直線 (“|”) ,雙引號 (“”) ,數位符號 (“#”) ,貨幣符號 (“$”) ,百分比符號 (“%”) “,ampersand (”&“) ,星號 (”*“) ,分號 (”;”) ,左角括弧 (“<”) ,右角括弧 (“>”“) ,左大括弧 (”{“) ,右大括弧 (”}“) , 左方括弧 (”[“) ,右方括弧 (”]“) ,等號 (”=“) ,at sign (”@“) ,圓角輔色 (”^“) ,底線 (”_“) ,以及 (”'“) 。

注意

UTF7Encoding 不提供錯誤偵測。 基於安全性考慮,建議您使用 UTF8EncodingUnicodeEncodingUTF32Encoding ,並啟用錯誤偵測。

適用於