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

Examples

The following code example demonstrates how to create a new UTF7Encoding instance and display the name of the encoding.

using System;
using System.Text;

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

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

.NET 9 and other versions
Product Versions (Obsolete)
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1 (5, 6, 7, 8, 9)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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);
[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);

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 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();
    }
}

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

.NET 9 and other versions
Product Versions (Obsolete)
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1 (5, 6, 7, 8, 9)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0