Export (0) Print
Expand All

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)

'Declaration
Public Sub New ( _
	encoderShouldEmitUTF8Identifier As Boolean, _
	throwOnInvalidBytes As Boolean _
)

Parameters

encoderShouldEmitUTF8Identifier
Type: System.Boolean

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

throwOnInvalidBytes
Type: System.Boolean

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.

NoteNote

For security reasons, your applications are recommended to use this constructor to create an instance of the UTF8Encoding class and turn on error detection by setting throwOnInvalidBytes to true.

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.

Imports System
Imports System.Text
Imports Microsoft.VisualBasic
'Imports Microsoft.VisualBasic.Strings 

Class UTF8EncodingExample

    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim utf8ThrowException As New UTF8Encoding(False, True)

        ' This array contains two high surrogates in a row: 
        ' ChrW(55297) and ChrW(55298). 
        ' A high surrogate should be followed by a low surrogate. 
        Dim chars() As Char = {"a"c, "b"c, "c"c, ChrW(55297), ChrW(55298), "d"c}

        ' The following method call will not throw an exception. 
        Dim bytes As Byte() = utf8.GetBytes(chars)
        ShowArray(bytes)

        Try 
            ' The following method call will throw an exception.
            bytes = utf8ThrowException.GetBytes(chars)
        Catch e As Exception
            Console.WriteLine("Exception raised. " + ControlChars.Cr + "Message: {0}", e.Message)
        End Try 
    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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft