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

UnicodeEncoding Constructor (Boolean, Boolean)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

public UnicodeEncoding(
	bool bigEndian,
	bool byteOrderMark
)

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 a Unicode byte order mark is provided; otherwise, false.

It is generally more efficient to encode Unicode characters using the native byte order. For example, it is better to use the little-endian byte order on little-endian platforms, such as Intel computers, and the big-endian byte order on big-endian platforms.

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

NoteNote:

For security reasons, your applications are recommended to enable error detection by using the constructor that accepts a throwOnInvalidBytes parameter and setting that parameter to true.

The following code 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.


using System;
using System.Text;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Create a UnicodeEncoding without parameters.
      UnicodeEncoding unicode = new UnicodeEncoding();

      // Create a UnicodeEncoding to support little-endian byte ordering
      // and include the Unicode byte order mark.
      UnicodeEncoding unicodeLittleEndianBOM =
          new UnicodeEncoding(false, true);
      // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
      DescribeEquivalence(outputBlock, unicode.Equals(unicodeLittleEndianBOM));

      // Create a UnicodeEncoding to support little-endian byte ordering
      // and not include the Unicode byte order mark.
      UnicodeEncoding unicodeLittleEndianNoBOM =
          new UnicodeEncoding(false, false);
      // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
      DescribeEquivalence(outputBlock, unicode.Equals(unicodeLittleEndianNoBOM));

      // Create a UnicodeEncoding to support big-endian byte ordering
      // and include the Unicode byte order mark.
      UnicodeEncoding unicodeBigEndianBOM =
          new UnicodeEncoding(true, true);
      // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
      DescribeEquivalence(outputBlock, unicode.Equals(unicodeBigEndianBOM));

      // Create a UnicodeEncoding to support big-endian byte ordering
      // and not include the Unicode byte order mark.
      UnicodeEncoding unicodeBigEndianNoBOM =
          new UnicodeEncoding(true, false);
      // Compare this UnicodeEncoding to the UnicodeEncoding without parameters.
      DescribeEquivalence(outputBlock, unicode.Equals(unicodeBigEndianNoBOM));
   }

   public static void DescribeEquivalence(System.Windows.Controls.TextBlock outputBlock, Boolean isEquivalent)
   {
      outputBlock.Text += String.Format(
          "{0} equivalent encoding.", (isEquivalent ? "An" : "Not an")
      ) + "\n";
   }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft