Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
UnicodeEncoding Constructor (Boolean, Boolean)

UnicodeEncoding Constructor (Boolean, Boolean)

Initializes a new instance of the UnicodeEncoding class. Parameters specify whether to use the big endian byte order and whether the GetPreamble method returns a Unicode byte order mark.

Namespace:  System.Text
Assemblies:   System.Text.Encoding.Extensions (in System.Text.Encoding.Extensions.dll)
  mscorlib (in mscorlib.dll)

'Declaration
Public Sub New ( _
	bigEndian As Boolean, _
	byteOrderMark As Boolean _
)

Parameters

bigEndian
Type: System.Boolean

true to use the big endian byte order (most significant byte first), or false to use the little endian byte order (least significant byte first).

byteOrderMark
Type: System.Boolean

true to specify that the GetPreamble method returns a Unicode byte order mark; otherwise, false. See the Remarks section for more information.

This constructor creates an instance that does not throw an exception when an invalid encoding is detected.

NoteNote

For security reasons, you should enable error detection by calling the UnicodeEncoding(Boolean, Boolean, Boolean) constructor and setting its throwOnInvalidBytes argument to true.

The byteOrderMark parameter controls the operation of the GetPreamble method. If true, the method returns a byte array containing the Unicode byte order mark (BOM) in UTF-16 format. If false, it returns a zero-length byte array. However, setting byteOrderMark to true does not cause the GetBytes method to prefix the BOM at the beginning of the byte array, nor does it cause the GetByteCount method to include the number of bytes in the BOM in the byte count.

The following example demonstrates how to create a new UnicodeEncoding instance specifying whether to support little endian or big endian byte ordering and the Unicode byte order mark.

Imports System
Imports System.Text

Class UnicodeEncodingExample

    Public Shared Sub Main()

        ' Create a UnicodeEncoding without parameters. 
        Dim unicodeDefault As New UnicodeEncoding()

        ' Create a UnicodeEncoding to support little-endian byte ordering 
        ' and include the Unicode byte order mark.         
        Dim unicodeLittleEndianBOM As New UnicodeEncoding(False, True)
        ' Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
        DescribeEquivalence(unicodeDefault.Equals(unicodeLittleEndianBOM))

        ' Create a UnicodeEncoding to support little-endian byte ordering 
        ' and not include the Unicode byte order mark. 
        Dim unicodeLittleEndianNoBOM As New UnicodeEncoding(False, False)
        ' Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
        DescribeEquivalence(unicodeDefault.Equals(unicodeLittleEndianNoBOM))

        ' Create a UnicodeEncoding to support big-endian byte ordering 
        ' and include the Unicode byte order mark. 
        Dim unicodeBigEndianBOM As New UnicodeEncoding(True, True)
        ' Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
        DescribeEquivalence(unicodeDefault.Equals(unicodeBigEndianBOM))

        ' Create a UnicodeEncoding to support big-endian byte ordering 
        ' and not include the Unicode byte order mark. 
        Dim unicodeBigEndianNoBOM As New UnicodeEncoding(True, False)
        ' Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
        DescribeEquivalence(unicodeDefault.Equals(unicodeBigEndianNoBOM))
    End Sub 


    Public Shared Sub DescribeEquivalence(isEquivalent As Boolean)
        Dim phrase as String 
        If isEquivalent Then
            phrase = "An" 
        Else
            phrase = "Not an" 
        End If
        Console.WriteLine("{0} equivalent encoding.", phrase)
    End Sub 
End Class

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

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

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft