Επεξεργασία

Κοινή χρήση μέσω


UTF7Encoding Constructors

Definition

Initializes a new instance of the UTF7Encoding class.

Overloads

UTF7Encoding()
Obsolete.

Initializes a new instance of the UTF7Encoding class.

UTF7Encoding(Boolean)
Obsolete.

Initializes a new instance of the UTF7Encoding class. A parameter specifies whether to allow optional characters.

UTF7Encoding()

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

Caution

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

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 ()
Attributes

Examples

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

Remarks

This constructor creates an instance that does not allow optional characters. Calling the UTF7Encoding constructor is equivalent to calling the UTF7Encoding.UTF7Encoding(Boolean) constructor that takes an allowOptionals parameter and specifying false for that parameter.

If an instance allows optional characters, Unicode code points are encoded with a corresponding optional character instead of a modified base 64 character. 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 ("`").

Note

UTF7Encoding does not provide error detection. For security reasons, your applications are recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

Applies to

UTF7Encoding(Boolean)

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

Caution

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

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)

Parameters

allowOptionals
Boolean

true to specify that optional characters are allowed; otherwise, false.

Attributes

Examples

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

Remarks

If an instance allows optional characters, Unicode code points are encoded with a corresponding optional character instead of a modified base 64 character. 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 ("`").

Note

UTF7Encoding does not provide error detection. For security reasons, your applications are recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

Applies to