UnicodeEncoding Constructor
Collapse the table of content
Expand the table of content

UnicodeEncoding Constructor

.NET Framework 1.1

Initializes a new instance of the UnicodeEncoding class.

Overload List

Initializes a new instance of the UnicodeEncoding class.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public UnicodeEncoding();
[C++] public: UnicodeEncoding();
[JScript] public function UnicodeEncoding();

Initializes a new instance of the UnicodeEncoding class, specifying whether to support little-endian or big-endian byte ordering and the Unicode byte order mark.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Boolean, Boolean)
[C#] public UnicodeEncoding(bool, bool);
[C++] public: UnicodeEncoding(bool, bool);
[JScript] public function UnicodeEncoding(Boolean, Boolean);

Example

[Visual Basic, C#, C++] 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. Depending on the values of the bigEndian and byteOrderMark parameters, the UnicodeEncoding instance behaves differently.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the UnicodeEncoding constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
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

[C#] 
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {

        // 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(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(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(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(unicode.Equals(unicodeBigEndianNoBOM));
    }

    public static void DescribeEquivalence(Boolean isEquivalent) {
        Console.WriteLine(
            "{0} equivalent encoding.", (isEquivalent ? "An" : "Not an")
        );
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

void DescribeEquivalence(Boolean isEquivalent)
{
   Console::WriteLine(S" {0} equivalent encoding.", (isEquivalent ? S"An" : S"Not an"));
}

int main()
{
   // 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(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 with->Item[Out] parameters.

   DescribeEquivalence(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(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(unicode -> Equals(unicodeBigEndianNoBOM));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

UnicodeEncoding Class | UnicodeEncoding Members | System.Text Namespace

Show:
© 2016 Microsoft