UTF8Encoding Constructor (Boolean, Boolean)
Collapse the table of content
Expand the table of content

UTF8Encoding Constructor (Boolean, Boolean)

Initializes a new instance of the UTF8Encoding class. Parameters specify whether to provide a Unicode byte order mark and whether to throw an exception when an invalid encoding is detected.

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)

public UTF8Encoding (
	bool encoderShouldEmitUTF8Identifier,
	bool throwOnInvalidBytes
)
public UTF8Encoding (
	boolean encoderShouldEmitUTF8Identifier, 
	boolean throwOnInvalidBytes
)
public function UTF8Encoding (
	encoderShouldEmitUTF8Identifier : boolean, 
	throwOnInvalidBytes : boolean
)

Parameters

encoderShouldEmitUTF8Identifier

true to specify that a Unicode byte order mark is provided; otherwise, false.

throwOnInvalidBytes

true to specify that an exception be thrown when an invalid encoding is detected; otherwise, false.

If throwOnInvalidBytes is true, a method that detects an invalid byte sequence throws System.ArgumentException; otherwise, the method does not throw an exception, and the invalid sequence is ignored.

Caution noteCaution

For security reasons, it is recommended that you use this constructor to create an instance of the UTF8Encoding class and turn on error detection by setting throwOnInvalidBytes to true.

Optionally, the UTF8Encoding provides a preamble, which is an array of bytes that you can prefix to the sequence of bytes resulting from the encoding process. If the preamble contains a byte order mark (code point U+FEFF), it helps the decoder determine the byte order and the transformation format or UTF. The Unicode byte order mark is serialized as EF BB BF (in hexadecimal). The GetPreamble method returns an array of bytes containing the byte order mark.

For more information on Unicode encoding, byte order, and the byte order mark, see The Unicode Standard at www.unicode.org.

The following example demonstrates how to create a new UTF8Encoding instance, specifying that a Unicode byte order mark prefix should not be emitted when encoding and an exception should be thrown when an invalid encoding is detected. The behavior of this constructor is compared to the default constructor for a UTF8Encoding, which does not throw an exception when an invalid encoding is detected.

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        UTF8Encoding utf8ThrowException = new UTF8Encoding(false, true);

        // This array contains two high surrogates in a row (\uD801, \uD802).
        // A high surrogate should be followed by a low surrogate.
        Char[] chars = new Char[] {'a', 'b', 'c', '\uD801', '\uD802', 'd'};

        // The following method call will not throw an exception.
        Byte[] bytes = utf8.GetBytes(chars);
        ShowArray(bytes);

        try {
            // The following method call will throw an exception.
            bytes = utf8ThrowException.GetBytes(chars);
        } catch (Exception e) {
            Console.WriteLine("Exception raised. \nMessage: {0}", e.Message);
        }
    }

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

import System.*;
import System.Text.*;

class UTF8EncodingExample
{
    public static void main(String[] args)
    {
        UTF8Encoding utf8 =  new UTF8Encoding();
        UTF8Encoding utf8ThrowException =  new UTF8Encoding(false, true);
      
        // This array contains two high surrogates in a row (\uD801, \uD802).
        // A high surrogate should be followed by a low surrogate.
        char chars[] = new char[] { 'a', 'b', 'c', '\uD801', '\uD802', 'd' };
   
        // The following method call will not throw an exception.
        ubyte bytes[] = utf8.GetBytes(chars);
           ShowArray(bytes);
        try {
             // The following method call will throw an exception.
            bytes = utf8ThrowException.GetBytes(chars);
        }
        catch(System.Exception  e) {
            Console.WriteLine("Exception raised. \nMessage: {0}", 
            e.get_Message());
        } 
    } //main
   
    public static void ShowArray(Array theArray) 
    {
        Object o = null;        
        for (int iCtr=0; iCtr < theArray.get_Count(); iCtr++) {
            o = theArray.get_Item(iCtr);
            Console.Write("[{0}]", o);
        }
        Console.WriteLine();
    } //ShowArray
} //UTF8EncodingExample

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft