UTF8Encoding Constructor (Boolean, Boolean)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

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

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.

Remarks

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.

Examples

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.Text
'Imports Microsoft.VisualBasic.Strings

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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(outputBlock, bytes)

      Try
         ' The following method call will throw an exception.
         bytes = utf8ThrowException.GetBytes(chars)
      Catch e As Exception
         outputBlock.Text += String.Format("Exception raised. " + ControlChars.Cr + "Message: {0}", e.Message) & vbCrLf
      End Try
   End Sub


   Public Shared Sub ShowArray(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal theArray As Array)
      Dim o As Object
      For Each o In theArray
         outputBlock.Text += String.Format("[{0}]", o)
      Next o
      outputBlock.Text &= vbCrLf
   End Sub
End Class
using System;
using System.Text;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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(outputBlock, bytes);

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

   public static void ShowArray(System.Windows.Controls.TextBlock outputBlock, Array theArray)
   {
      foreach (Object o in theArray)
      {
         outputBlock.Text += String.Format("[{0}]", o);
      }
      outputBlock.Text += "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.